A library to use I2C analog IC with Arduino and ESP8266. It can read analog values and write analog values with only 2 wires (perfect for ESP-01).
You can find updated version of my library on my site https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/Tutorial:To download, click the DOWNLOADS button in the top right corner and rename the uncompressed folder PCF8591. Check that the PCF8591 folder contains PCF8591\\.cpp
and PCF8591.h
. Place the DHT library folder your <arduinosketchfolder>/libraries/
folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
Set up the PCF8591 analog input and analog output with the digital converter with I2C bus. I try to simplify the use of this IC, with a minimal set of operations. Constructor: you must pass the address of I2C (to check the address use this guide I2cScanner). For ESP8266 if you want to specify SDA and SCL pins, use this. Then IC, as you can see in the image, has 4 analog inputs and 1 analog output: So to read all analog inputs in one transmission you can do this (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, this IC has multiple types of read and you can use analog input or analog channel (when you use the same single read analog input and channel):
For example, to read the value of channel 0 in two differential inputs, try the following methods if you want write an analog value (with the value ranging from 0 to 255). An additional feature is to read a write voltage. For the calculation of voltage, you must pass some parameters:
microcontrollerReferenceVoltage
: get voltage from the microcontroller (only AVR, no ESP8266 for esp 3.3v fixed).referenceVoltage
: ifmicrocontrollerReferenceVoltage
is false, take this value.
The commands 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 example is:
pcf8591.voltageWrite(2.7); // 2.7Volts output
delay(3000);
float ana0V = pcf8591.voltageRead(AIN0); // Read voltage from analog 0
Serial.println(ana0V);
For the example, I use this wire schematic on the breadboard.
Comments