Skip to content

Commit

Permalink
Add support for esp32 second channel i2c
Browse files Browse the repository at this point in the history
  • Loading branch information
xreef committed Apr 29, 2019
1 parent c400468 commit 5f170f3
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 25 deletions.
102 changes: 80 additions & 22 deletions PCF8591.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/**
* PCF8591 Analog Port Expand
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "PCF8591.h"
#include "Wire.h"

Expand All @@ -6,33 +33,64 @@
* @param address: i2c address
*/
PCF8591::PCF8591(uint8_t address){
_wire = &Wire;

_address = address;
};
#ifndef __AVR
#if !defined(__AVR) && !defined(__STM32F1__)
/**
*
* @param address: i2c address
* @param sda: sda pin
* @param scl: scl pin
*/
PCF8591::PCF8591(uint8_t address, uint8_t sda, uint8_t scl){
_wire = &Wire;

_address = address;
_sda = sda;
_scl = scl;
};

#ifdef ESP32
/**
* Constructor
* @param address: i2c address
*/
PCF8591::PCF8591(TwoWire *pWire, uint8_t address){
_wire = pWire;

_address = address;
};
/**
*
* @param address: i2c address
* @param sda: sda pin
* @param scl: scl pin
*/
PCF8591::PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl){
_wire = pWire;

_address = address;
_sda = sda;
_scl = scl;
};

#endif

#endif

/**
* wake up i2c controller
*/
void PCF8591::begin(){
#ifndef __AVR
Wire.begin(_sda, _scl);
_wire->begin(_sda, _scl);
#else
// Default pin for AVR some problem on software emulation
// #define SCL_PIN _scl
// #define SDA_PIN _sda
Wire.begin();
_wire->begin();
#endif
}

Expand All @@ -47,22 +105,22 @@ void PCF8591::begin(){
*/
struct PCF8591::AnalogInput PCF8591::analogReadAll(byte readType){
DEBUG_PRINTLN("Begin trasmission");
Wire.beginTransmission(_address); // wake up PCF8591
_wire->beginTransmission(_address); // wake up PCF8591

byte operation = AUTOINCREMENT_READ | readType | (_outputStatus&OUTPUT_MASK);

DEBUG_PRINTLN("Write operation");
Wire.write(operation); // control byte: reads ADC0 then auto-increment
_wire->write(operation); // control byte: reads ADC0 then auto-increment
DEBUG_PRINTLN("End write (If code stop here add pullup resistor on SDA SCL)");
Wire.endTransmission(); // end tranmission
_wire->endTransmission(); // end tranmission
DEBUG_PRINTLN("Request response");
Wire.requestFrom(_address, (uint8_t)5);
_wire->requestFrom(_address, (uint8_t)5);

/*uint8_t control =*/Wire.read();
analogInput.ain0=Wire.read();
analogInput.ain1=Wire.read();
analogInput.ain2=Wire.read();
analogInput.ain3=Wire.read();
/*uint8_t control =*/_wire->read();
analogInput.ain0=_wire->read();
analogInput.ain1=_wire->read();
analogInput.ain2=_wire->read();
analogInput.ain3=_wire->read();

return analogInput;
};
Expand All @@ -79,18 +137,18 @@ struct PCF8591::AnalogInput PCF8591::analogReadAll(byte readType){
*/
uint8_t PCF8591::analogRead(uint8_t channel, byte readType){
DEBUG_PRINTLN("Begin trasmission");
Wire.beginTransmission(_address); // wake up PCF8591
_wire->beginTransmission(_address); // wake up PCF8591

byte operation = channel | readType| (_outputStatus&OUTPUT_MASK);

DEBUG_PRINTLN("Write operation");
Wire.write(operation); // control byte: reads ADC0 then auto-increment
_wire->write(operation); // control byte: reads ADC0 then auto-increment
DEBUG_PRINTLN("End write (If code stop here add pullup resistor on SDA SCL)");
Wire.endTransmission(); // end tranmission
_wire->endTransmission(); // end tranmission
DEBUG_PRINTLN("Request response");
Wire.requestFrom(_address, (uint8_t)2);
/*uint8_t control = */Wire.read();
uint8_t ana=Wire.read();
_wire->requestFrom(_address, (uint8_t)2);
/*uint8_t control = */_wire->read();
uint8_t ana=_wire->read();

return ana;
};
Expand Down Expand Up @@ -127,13 +185,13 @@ void PCF8591::voltageWrite(float value, bool microcontrollerReferenceVoltage, fl
void PCF8591::analogWrite(uint8_t value){
_outputStatus = ENABLE_OUTPUT;
DEBUG_PRINTLN("Begin trasmission");
Wire.beginTransmission(_address);
_wire->beginTransmission(_address);
DEBUG_PRINTLN("Write operation");
Wire.write(ENABLE_OUTPUT); // sets the PCF8591 into a DA mode
_wire->write(ENABLE_OUTPUT); // sets the PCF8591 into a DA mode
DEBUG_PRINTLN("Write value");
Wire.write(value); // sets the output
_wire->write(value); // sets the output
DEBUG_PRINTLN("End write (If code stop here add pullup resistor on SDA SCL)");
Wire.endTransmission();
_wire->endTransmission();
};

long PCF8591::readVcc(void) {
Expand Down
35 changes: 32 additions & 3 deletions PCF8591.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
/** \mainpage PCF8591 library
* PCF8591 Analog Port Expand
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
* MIT license
* written by Renzo Mischianti
* The MIT License (MIT)
*
* Copyright (c) 2017 Renzo Mischianti www.mischianti.org All right reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef PCF8591_h
Expand Down Expand Up @@ -65,8 +86,14 @@ class PCF8591 {

PCF8591(uint8_t address);

#ifndef __AVR
#if !defined(__AVR) && !defined(__STM32F1__)
PCF8591(uint8_t address, uint8_t sda, uint8_t scl);

#ifdef ESP32
PCF8591(TwoWire *pWire, uint8_t address);
PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl);
#endif

#endif

void begin(void);
Expand All @@ -78,6 +105,8 @@ class PCF8591 {
float voltageRead(uint8_t analogPin, bool microcontrollerReferenceVoltage = true, float referenceVoltage = 5.0);

private:
TwoWire *_wire;

uint8_t _address;
uint8_t _sda = SDA;
uint8_t _scl = SCL;
Expand Down
18 changes: 18 additions & 0 deletions examples/analogReadWrite/analogReadWrite.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* PCF8591 Analog Port Expand
* Read all analog pins and write value on analog ouput
*
* by Mischianti Renzo <http://www.mischianti.org>
*
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
*
* PCF8574 ----- Esp32
* A0 ----- GRD
* A1 ----- GRD
* A2 ----- GRD
* SDA ----- A4
* SCL ----- A5
*
*
*/
#include "Arduino.h"
#include "PCF8591.h"
#define PCF8591_I2C_ADDRESS 0x48
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* PCF8591 Analog Port Expand
* Read all analog pins and write value on analog ouput
*
* by Mischianti Renzo <http://www.mischianti.org>
*
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
*
* PCF8574 ----- Esp32
* A0 ----- GRD
* A1 ----- GRD
* A2 ----- GRD
* SDA ----- 21
* SCL ----- 22
*
*
*/
#include "Arduino.h"

#include "PCF8591.h"
#define PCF8591_I2C_ADDRESS 0x48

// Instantiate Wire for generic use at 400kHz
TwoWire I2Cone = TwoWire(0);
// Instantiate Wire for generic use at 100kHz
TwoWire I2Ctwo = TwoWire(1);

// Set i2c address
//PCF8591 pcf8591(&I2Ctwo, PCF8591_I2C_ADDRESS);
PCF8591 pcf8591(&I2Ctwo, 0x20, 21, 22);

void setup()
{
Serial.begin(115200);
I2Cone.begin(16,17,400000); // SDA pin 16, SCL pin 17, 400kHz frequency

pcf8591.begin();
}

void loop()
{
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);

delay(3000);

int ana = pcf8591.analogRead(AIN0);
Serial.print("AIN0 --> ");
Serial.println(ana);

ana = pcf8591.analogRead(AIN1);
Serial.print("AIN1 --> ");
Serial.println(ana);

ana = pcf8591.analogRead(AIN2);
Serial.print("AIN2 --> ");
Serial.println(ana);

ana = pcf8591.analogRead(AIN3);
Serial.print("AIN3 --> ");
Serial.println(ana);
delay(3000);

pcf8591.analogWrite(0);
delay(3000);
pcf8591.analogWrite(128);
delay(3000);
pcf8591.analogWrite(255);
delay(3000);
}
42 changes: 42 additions & 0 deletions examples/generateSinusoidal/generateSinusoidal.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* PCF8591 Analog Port Expand
* Production of a sinusoïdal signal using a PCF8591 module
*
* by Yves Pelletier <http://electroniqueamateur.blogspot.com>
*
* http://electroniqueamateur.blogspot.com/2019/01/pcf8591-et-esp8266-ou-arduino.html
*
*
* PCF8574 ----- Esp32
* A0 ----- GRD
* A1 ----- GRD
* A2 ----- GRD
* SDA ----- A4
* SCL ----- A5
*
*
*/

#include "PCF8591.h" // bibliothèque https://github.com/xreef/PCF8591_library
#define PCF8591_I2C_ADDRESS 0x48 //adresse i2c du module PCF8591

PCF8591 pcf8591(PCF8591_I2C_ADDRESS);

int compteur;

void setup()
{
pcf8591.begin();
}

void loop(){
pcf8591.analogWrite(100 + 100 * sin(2*3.1416*compteur/200) ); // sinus

// pcf8591.analogWrite(compteur ); // dent de scie

compteur++;
if (compteur > 200){
compteur = 0;
}
delay(1);
}
18 changes: 18 additions & 0 deletions examples/voltageReadWrite/voltageReadWrite.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* PCF8591 Analog Port Expand
* Read write voltage on pins
*
* by Mischianti Renzo <http://www.mischianti.org>
*
* https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
*
*
* PCF8574 ----- Esp32
* A0 ----- GRD
* A1 ----- GRD
* A2 ----- GRD
* SDA ----- A4
* SCL ----- A5
*
*
*/
#include "Arduino.h"
#include "PCF8591.h"
#define PCF8591_I2C_ADDRESS 0x48
Expand Down

0 comments on commit 5f170f3

Please sign in to comment.