Using the PCF8591 with Arduino is a simple matter of wiring up it to your Arduino-compatible microcontroller, installing the Adafruit PCF8591 library we've written, and running the provided example code.

I2C Wiring

Wiring the PCF8591  is made simple by using the I2C interface. The default I2C address for the PCF8591 is 0x48 but it can be switched to several up to 0x4F by shorting the address jumpers. See the Pinouts page for details

  • Connect PCF8591 VCC (red wire) to Arduino 5V if you are running a 5V board Arduino (Uno, etc.). If your board is 3V, connect to that instead.
  • Connect PCF8591 GND (black wire) to Arduino GND
  • Connect PCF8591 SCL (yellow wire) to Arduino SCL
  • Connect PCF8591 SDA (blue wire) to Arduino SDA

For our test program, we expect you'll wire up the ADC / DAC pins as well:

  • Use a jumper wire to connect the PCF8591's A0 pin to the Out pin
  • Use another wire to connect PCF8591's A1 pin to the Arduino 3.3V pin.
  • Connect PCF8591 A2 pin to Arduino IORef
  • Connect PCF8691 A3 to Arduino GND

Library Installation

You can install the Adafruit PCF8591 library for Arduino using the Library Manager in the Arduino IDE.

Click the Manage Libraries ... menu item, search for Adafruit PCF8591, and select the Adafruit PCF8591 library:

Follow the same process for the Adafruit BusIO library.

Load Example

Open up File -> Examples -> Adafruit PCF8591 -> PCF8591

After opening the demo file, upload to your Arduino wired up to the sensor. Once you upload the code, you will see the ADC values being printed when you open the Serial Monitor (Tools->Serial Monitor) at 115200 baud, similar to this:

Example Code

#include <Adafruit_PCF8591.h>
// Make sure that this is set to the value in volts of VCC
#define ADC_REFERENCE_VOLTAGE 5.0
Adafruit_PCF8591 pcf = Adafruit_PCF8591();

void setup() {
  Serial.begin(115200);
  while (!Serial)
    delay(10);

  Serial.println("# Adafruit PCF8591 demo");
  if (!pcf.begin()) {
    Serial.println("# Adafruit PCF8591 not found!");
    while (1)
      delay(10);
  }
  Serial.println("# Adafruit PCF8591 found");
  pcf.enableDAC(true);

  Serial.println("AIN0, AIN1, AIN2, AIN3");
}

uint8_t dac_counter = 0;

void loop() {
  // Make a triangle wave on the DAC output
  pcf.analogWrite(dac_counter++);

  Serial.print(int_to_volts(pcf.analogRead(0), 8, ADC_REFERENCE_VOLTAGE));
  Serial.print("V, ");
  Serial.print(int_to_volts(pcf.analogRead(1), 8, ADC_REFERENCE_VOLTAGE));
  Serial.print("V, ");
  Serial.print(int_to_volts(pcf.analogRead(2), 8, ADC_REFERENCE_VOLTAGE));
  Serial.print("V, ");
  Serial.print(int_to_volts(pcf.analogRead(3), 8, ADC_REFERENCE_VOLTAGE));
  Serial.print("V");
  Serial.println("");
  delay(1000);
}

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

This guide was first published on Jul 13, 2020. It was last updated on Jul 13, 2020.

This page (Arduino) was last updated on May 10, 2021.

Text editor powered by tinymce.