Hello,
I am trying to use two I2C buses on an ESP32. One of the buses is for talking between a number of ESP32's, so this code is for a slave device. Once requested it will send data from various sensors. My plan was to keep the Slave I2C bus on its own network pins 05 & 18, while using the ADS1115 on pins 21 & 22.
The issue I have is that once I add the following code the ADS1115 stops communicating correctly and the only output I get is -1 from the serial output.
bool res = WireSlave.begin(SDA_PIN, SCL_PIN, I2C_SLAVE_ADDR);
if (!res) {
Serial.println("I2C slave init failed");
while(1) delay(100);
}
Complete code below:
// I2C
#include <Arduino.h>
#include <Wire.h>
#include <WireSlave.h>
#include <WirePacker.h>
#include <WireSlaveRequest.h>
#define SDA_PIN 18
#define SCL_PIN 05
#define I2C_SLAVE_ADDR 0x08
void requestEvent();
/*-----( Strings to send )-----*/
String FlowMessage1;
String PressMessage1;
int FlowrateMain1 = 0;
int PressureMain1 = 0;
/*+++++++++++++++++++++++++++++++ 16bit ADS1115 & Flowmeter Info +++++++++++++++++++++++++++++++*/
#include <Adafruit_ADS1015.h>/*
0x48 (1001000) ADR -> GND
0x49 (1001001) ADR -> VDD
0x4A (1001010) ADR -> SDA
0x4B (1001011) ADR -> SCL
*/
Adafruit_ADS1115 ads1(0x48); /* Use this for the 16-bit version */
/*-----( Main Flowmeter Info )-----*/
int FlowrateMain = 0;
float FlowrateCFloat;
unsigned long startMillisMain1; //some global variables available anywhere in the program
unsigned long currentMillisMain1;
const unsigned long periodMain1 = 1000; //the value is a number of milliseconds
float VolumeMain1 = 0;
float TotalVolumeMain1 = 0.0;
int FlowMain1; // Current Flowrate
int TotalMain1; // Total Volume
float TotalMain1m3 = 0; // Total Volume in m3
void setup(void)
{
Serial.begin(115200);
bool res = WireSlave.begin(SDA_PIN, SCL_PIN, I2C_SLAVE_ADDR);
if (!res) {
Serial.println("I2C slave init failed");
while(1) delay(100);
}
WireSlave.onRequest(requestEvent);
Serial.printf("Slave joined I2C bus with addr #%d\n", I2C_SLAVE_ADDR);
delay(500);
Serial.println("Hello!");
Serial.println("Getting single-ended readings from AIN0..3");
Serial.println("ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV");
Wire.begin(21,22); // join i2c bus
ads1.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
uint32_t test = 100000;
ads1.begin();
}
void loop(void)
{
/*+++++++++++++++++++++++++++++++ Time +++++++++++++++++++++++++++++++*/
currentMillisMain1 = millis(); //get the current "time" (actually the number of milliseconds since the program started)
//Wire.begin(21, 22);
int16_t adc0;
adc0 = ads1.readADC_SingleEnded(0);
Serial.print("AIN0: ");
Serial.println(adc0);
FlowrateMain1 = adc0;
delay(1000);
}
// function that runs whenever the master sends an empty packet.
// this function is registered as an event, see setup().
// do not perform time-consuming tasks inside this function,
// do them elsewhere and simply read the data you wish to
// send inside here.
void requestEvent()
{
/*-----( Messages sent to motor controller arduino unit )-----*/
char Buf1[25]; // Creates a char call Buf1 of length 25
FlowMessage1 = String(31) + "," + String(FlowrateMain1) + "," + String(1) + "\n"; //Makes a string, code seperates at " , " and \n required for code to know when its got to the end
FlowMessage1.toCharArray(Buf1, 25); // Converts String to Char of length 25
char Buf2[25];
PressMessage1 = String(21) + "," + String(PressureMain1) + "," + String(1) + "\n";
PressMessage1.toCharArray(Buf2, 25);
Any help would be greatly appreciated