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ļ
In this lesson, youāll learn how to connect the Raspberry Pi Pico W with the PCF8591 ADC DAC Converter Module using MicroPython. Youāll establish an I2C connection, initialize the PCF8591 module, and read analog values from its channels. This hands-on session will deepen your grasp of analog-to-digital conversion and I2C communication on the Raspberry Pi Pico W. 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 Pico W |
|
Wiringļ
Codeļ
Note
Open the
10_pcf8591_module.pyfile under the path ofuniversal-maker-sensor-kit-main/pico/Lesson_10_PCF8591_Moduleor copy this code into Thonny, then click āRun Current Scriptā or simply press F5 to run it. For detailed tutorials, please refer to Open and Run Code Directly.Here you need to use the
PCF8591.py, please check if it has been uploaded to Pico W, for a detailed tutorial refer to Upload the Libraries to Pico.Donāt forget to click on the āMicroPython (Raspberry Pi Pico)ā interpreter in the bottom right corner.
from machine import I2C, Pin
import time
from PCF8591 import PCF8591
# Setup I2C connection on pins 20 (SDA) and 21 (SCL)
i2c = I2C(0, sda=Pin(20), scl=Pin(21))
# Initialize the PCF8591 module at address 0x48
pcf8591 = PCF8591(0x48, i2c) # Adjust the address if needed
# Check if the PCF8591 module is connected
if pcf8591.begin():
print("PCF8591 found")
# Main loop to read analog values
while True:
# Read and print the analog value from channel AIN0
AIN0 = pcf8591.analog_read(PCF8591.AIN0)
print("AIN0 ", AIN0) # PCF8591.CHANNEL_0 can also be used
# Additional channels can be read by uncommenting the following lines
# print("AIN1 ", pcf8591.analog_read(PCF8591.AIN1))
# print("AIN2 ", pcf8591.analog_read(PCF8591.AIN2))
# print("AIN3 ", pcf8591.analog_read(PCF8591.AIN3))
# Write the value back to AOUT. This will change the brightness of the D2 LED on the module.
pcf8591.analog_write(AIN0)
# Wait for 0.2 seconds before the next read
time.sleep(0.2)
Code Analysisļ
Importing Libraries and Setting Up I2C
The
machinemodule is imported to use I2C communication andPinclass.The
timemodule is imported for adding delays in the program.The
PCF8591library is imported for easy interaction with the PCF8591 module. For more information about thePCF8591library, please visit xreef/PCF8591_micropython_library.
from machine import I2C, Pin import time from PCF8591 import PCF8591
Initializing I2C Connection
I2C communication is initialized using SDA (Serial Data) and SCL (Serial Clock) pins. The Raspberry Pi Pico W uses GPIO 20 and 21 for this purpose.
i2c = I2C(0, sda=Pin(20), scl=Pin(21))
Initializing the PCF8591 Module
The PCF8591 module is initialized with its I2C address (0x48). This address might need adjustment depending on the moduleās configuration.
pcf8591 = PCF8591(0x48, i2c) # Adjust the address if needed
Checking Connection
The program checks if the PCF8591 module is connected correctly.
if pcf8591.begin(): print("PCF8591 found")
Main Loop for Reading Analog Values
The program enters an infinite loop, continuously reading the analog value from channel AIN0.
The
analog_readfunction is used to read the value from a specified channel.The
analog_writefunction is used to write the value to AOUT.Jumper caps link the moduleās potentiometer to AIN0, and the D2 LED is connected to AOUT. So the brightness of the LED changes as the potentiometer is rotated. Please refer to the PCF8591 module schematic for details.
A delay of 0.2 seconds is added between reads to stabilize the output.
while True: # Read and print the analog value from channel AIN0 AIN0 = pcf8591.analog_read(PCF8591.AIN0) print("AIN0 ", AIN0) # PCF8591.CHANNEL_0 can also be used # Additional channels can be read by uncommenting the following lines # print("AIN1 ", pcf8591.analog_read(PCF8591.AIN1)) # print("AIN2 ", pcf8591.analog_read(PCF8591.AIN2)) # print("AIN3 ", pcf8591.analog_read(PCF8591.AIN3)) # Write the value back to AOUT. This will change the brightness of the D2 LED on the module. pcf8591.analog_write(AIN0) # Wait for 0.2 seconds before the next read time.sleep(0.2)