Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 10: PCF8591 ADC DAC Converter Module
Note
The Raspberry Pi does not have analog input capabilities, so it needs a module like the PCF8591 ADC DAC Converter Module to read analog signals for processing.
In this lesson, you will learn how to use a Raspberry Pi to interact with the PCF8591 module for analog-to-digital and digital-to-analog conversion. We’ll cover reading analog values from input AIN0, sending these values to the DAC(AOUT). The module’s potentiometer is connected to AIN0 using jumper caps, and the D2 LED on the module is connected to AOUT, so you can see that the brightness of D2 LED changes as you rotate the potentiometer.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi 5 |
|
Wiring
Note
In this project, we utilized the AIN0 pin of the PCF8591 module, which is linked to a potentiometer on the module through a jumper cap. Please make sure that the jumper cap on the module is correctly placed. For more details, please refer to the PCF8591 module schematic.
Code
import PCF8591 as ADC # Import the library for the PCF8591 module
import time # Import the time library for adding delays
# Initialize the PCF8591 module at I2C address 0x48.
# This address is used for communication with the Raspberry Pi.
ADC.setup(0x48)
try:
while True: # Start an infinite loop to continuously monitor the sensor.
# Read the analog value from the potentiometer connected to AIN0.
# Channel range from 0 to 3 represents AIN0 to AIN3.
# The potentiometer's rotation alters the voltage, which is read by the PCF8591.
potentiometer_value = ADC.read(0)
print(potentiometer_value)
# Write the value back to AOUT. This will change the brightness of the D2 LED on the module.
# LED won't light up below 80, so convert '0-255' to '80-255'
# As the potentiometer is adjusted, the LED's brightness varies proportionally.
tmp = potentiometer_value*(255-80)/255+80
ADC.write(tmp)
# Add a short delay of 0.2 seconds to make the loop more manageable.
time.sleep(0.2)
except KeyboardInterrupt:
# If a KeyboardInterrupt (CTRL+C) is detected, exit the loop and end the program.
print("Exit")
Code Analysis
Importing Libraries:
The script starts by importing required libraries. The
PCF8591library is used for interacting with the ADC/DAC module, andtimeis for creating delays.import PCF8591 as ADC # Import the library for the PCF8591 module import time # Import the time library for adding delays
Initializing PCF8591 Module:
The PCF8591 module is initialized at the I²C address 0x48. This step is crucial for setting up communication between the Raspberry Pi and the module.
ADC.setup(0x48) # Initialize the PCF8591 module at I2C address 0x48
Reading from Potentiometer and Writing to LED:
Within a
tryblock, a continuouswhile Trueloop reads the value from the potentiometer connected to AIN0 and writes this value to the DAC connected to AOUT. Jumper caps link the module’s potentiometer to AIN0, and the D2 LED is connected to AOUT; please refer to the PCF8591 module schematic for details. The brightness of the LED changes as the potentiometer is rotated.Use
ADC.read(channel)to read the analog input of the specific channel. The channel range from 0 to 3 represents AIN0 to AIN3.Use
ADC.write(Value)to set the analog output of the AOUT pin with a Value range from 0 to 255.
try: while True: # Start an infinite loop to continuously monitor the sensor. potentiometer_value = ADC.read(0) print(potentiometer_value) tmp = potentiometer_value*(255-80)/255+80 ADC.write(tmp) time.sleep(0.2)
Handling Keyboard Interrupts:
A
KeyboardInterrupt(such as pressing CTRL+C) allows for a graceful exit from the loop without generating errors.except KeyboardInterrupt: print("Exit")