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 ESP32 Development Board with a PCF8591 ADC DAC Converter Module. We’ll cover reading analog values from input AIN0, sending these values to the DAC(AOUT), and displaying both the raw and voltage-converted readings on the serial monitor. 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

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

PCF8591 ADC DAC Converter Module

BUY

Breadboard

BUY

Wiring

../_images/Lesson_10_PCF8591_Module_esp32_bb.png

Code

Note

To install the library, use the Arduino Library Manager and search for “Adafruit PCF8591” and install it.

Code Analysis

  1. Including the Library and Defining Constants

    Note

    To install the library, use the Arduino Library Manager and search for “Adafruit PCF8591” and install it.

    // Include Adafruit PCF8591 library
    #include <Adafruit_PCF8591.h>
    // Define the reference voltage for ADC conversion
    #define ADC_REFERENCE_VOLTAGE 3.3
    

    This section includes the Adafruit PCF8591 library, which provides functions for interacting with the PCF8591 module. The ADC reference voltage is set to 3.3 volts, which is the maximum voltage that the ADC can measure.

  2. Setting Up the PCF8591 Module

    // Create an instance of the PCF8591 module
    Adafruit_PCF8591 pcf = Adafruit_PCF8591();
    void setup() {
      Serial.begin(9600);
      Serial.println("# Adafruit PCF8591 demo");
      if (!pcf.begin()) {
        Serial.println("# PCF8591 not found!");
        while (1) delay(10);
      }
      Serial.println("# PCF8591 found");
      pcf.enableDAC(true);
    }
    

    In the setup function, serial communication is started, and an instance of the PCF8591 module is created. The pcf.begin() function checks if the module is connected properly. If not, it prints an error message and halts the program. If the module is found, it enables the DAC.

  3. Reading from ADC and Writing to DAC

    void loop() {
      AIN0 = pcf.analogRead(0);
      pcf.analogWrite(AIN0);
      Serial.print("AIN0: ");
      Serial.print(AIN0);
      Serial.print(", ");
      Serial.print(int_to_volts(AIN0, 8, ADC_REFERENCE_VOLTAGE));
      Serial.println("V");
      delay(500);
    }
    

    The loop function continuously reads the analog value from AIN0 (analog input 0) of the PCF8591 module, then writes this value back to the DAC. It also prints the raw value and the voltage-converted value of AIN0 to the Serial Monitor.

    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.

  4. Digital to Voltage Conversion Function

    float int_to_volts(uint16_t dac_value, uint8_t bits, float logic_level) {
      return (((float)dac_value / ((1 << bits) - 1)) * logic_level);
    }
    

    This function converts the digital value back to its corresponding voltage. It takes the digital value (dac_value), the number of bits of resolution (bits), and the logic level voltage (logic_level) as arguments. The formula used is a standard approach to convert a digital value to its equivalent voltage.