PCF8591 (i2c Analog I/O Expander) Fast Easy Usage

302

Posted

Introduction: PCF8591 (i2c Analog I/O Expander) Fast Easy Usage

Library to use i2c pcf8591 IC with arduino and esp8266.

This IC can control (until 4) analog input and/or 1 analog output like measure voltage, read thermistor value or fade a led.

Can read analog value and write analog value with only 2 wire (perfect for ESP-01).

I try to simplify the use of this IC, with a minimal set of operation.

Step 1: How I2c Works

I2C works with it's two wires, the SDA(data line) and SCL(clock line).

Both these lines are open-drain, but are pulled-up with resistors.

Usually there is one master and one or multiple slaves on the line, although there can be multiple masters, but we'll talk about that later.

Both masters and slaves can transmit or receive data, therefore, a device can be in one of these four states: master transmit, master receive, slave transmit, slave receive.

Step 2:

You can find my library here.

To download.

Click the DOWNLOADS button in the top right corner, rename the uncompressed folder PCF8591.

Check that the PCF8591 folder contains PCF8591.cpp and PCF8591.h.

Place the PCF8591 library folder your /libraries/ folder.

You may need to create the libraries subfolder if its your first library.

Restart the IDE.

Step 3: Usage

Constructor: you must pas the address of i2c (to check the adress use this guide I2cScanner)

	PCF8591(uint8_t address); 

for esp8266 if you want specify SDA e SCL pin use this:

	PCF8591(uint8_t address, uint8_t sda, uint8_t scl);

Step 4: Read Value

IC as you can see in the image have 4 analog input and 1 analog output.

So to read all analog input in one trasmission you can do (the value is from 0 to 255):

	PCF8591::AnalogInput ai = pcf8591.analogReadAll();
	Serial.print(ai.ain0);
	Serial.print(" - ");
	Serial.print(ai.ain1);
	Serial.print(" - ");
	Serial.print(ai.ain2);
	Serial.print(" - ");
	Serial.println(ai.ain3); 

if you want read a single analog input or channel:

	int ana = pcf8591.analogRead(AIN0); // read analog 0

Step 5: Read Vale From Channel

This IC have multiple type of read and you can use Analog input or analog channel (when you use single read analog input and channel are in the pictures).

For example to read the value of channel 0 in Two differential input you must do:

	int ana = pcf8591.analogRead(CHANNEL0, TWO_DIFFERENTIAL_INPUT); // read analog 0

Step 6: Write Value

If you want write an analog value you must do (the value is from 0 to 255):

	pcf8591.analogWrite(128);

Step 7: Additional Features

Additional feature is to read a write voltage: For the calculation of voltage you must pass some parameter:

  • microcontrollerReferenceVoltage: get voltage from microcontroller voltage (only AVR no esp8266 for esp 3.3v fixed)
  • referenceVoltage: if microcontrollerReferenceVoltage false take this valueThe command are:

	void voltageWrite(float value, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);
	float voltageRead(uint8_t analogPin, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);

An examples is:

	pcf8591.voltageWrite(2.7); // 2.7Volts output
	delay(3000);
	float ana0V = pcf8591.voltageRead(AIN0); // Read voltage from analog 0
	Serial.println(ana0V);

Step 8: Examples Connection Diagram

Step 9: Thanks

Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Spotless Contest

      Spotless Contest
    • Science of Cooking

      Science of Cooking
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments