johnquoc
Please help me fix this code, I want to read analog input from ads 1015 via I2C and point data to 
pinMask_AIN and I connected my board to a voltage and current signal generator, When I increased the voltage, AIN in the openPLC editor did not change
[203128%2F8432554%2Fmceclip0]
[203128%2F8432557%2Fmceclip0]
[203128%2F8432558%2Fmceclip0]
[203128%2F8432560%2Fmceclip0]
[203128%2F8432561%2Fmceclip0]
Reply 0 0
thiagoralves
Your approach has several problems
 
1. You must disable the pins you're using for the I2C communication, otherwise these pins might be used be the regular driver and mess up your I2C communication. See https://github.com/thiagoralves/OpenPLC_Editor/blob/master/editor/arduino/src/hal/esp32.cpp at function init_pwm() for an example on how to disable pins
 
2. Your updateInputBuffers loop is wrong. You're iterating over NUM_ANALOG_INPUT which is a constant defined dynamically depending on how many pins the user has selected as analog inputs. The correct approach would be to leave the loop alone as it is originally, and then iterate the loop over NUM_ANALOG_INPUT + n, where "n" are the extra analog inputs you want to add through I2C. If the current loop variable i is over NUM_ANALOG_INPUT then it means you're now reading from your I2C instead of regular analog pins. The result will be a combination of both regular analog pins defined for the ESP32 plus your extra I2C analog inputs, all mapped to %IW locations.
 
3. Make sure your code is compiling correctly, as it seems you're assigning a float to *int_input[i]
 
4. Next time you want to share code, use the code button here in the forum and copy/paste your code. It will take less time for you and work a lot better than screenshot snippets that don't give full code visibility
Reply 0 0
johnquoc
This is the code that I fixed, please help me correct this code.
#include <stdlib.h>
extern "C" {
#include "openplc.h"
}
#include "Arduino.h"
#include "../examples/Baremetal/defines.h"
//#include "SoftwareI2C.h"
#include "Wire.h"
#include <Adafruit_ADS1015.h>
//#include <DAC.h>

//OpenPLC HAL for ESP32 boards

/******************PINOUT CONFIGURATION**************************
Digital In:  17, 18, 19, 21, 22, 23, 27, 32 (%IX0.0 - %IX0.7)
             33                             (%IX1.0 - %IX1.0)
Digital Out: 01, 02, 03, 04, 05, 12, 13, 14 (%QX0.0 - %QX0.7)
             15, 16                         (%QX1.0 - %QX1.1)
Analog In:   34, 35, 36, 39                 (%IW0 - %IW2)
Analog Out:  25, 26                         (%QW0 - %QW1)
*****************************************************************/

//Create the I/O pin masks
#define NUM_ADC_CHANNELS 4
//int16_t adcBuffer[NUM_ADC_CHANNELS];
uint8_t pinMask_DIN[] = {PINMASK_DIN};
uint8_t pinMask_AIN[] = {PINMASK_AIN};  
uint8_t pinMask_DOUT[] = {PINMASK_DOUT};
uint8_t pinMask_AOUT[] = {PINMASK_AOUT};

#define PIN_SDA 15
#define PIN_SCL 13

Adafruit_ADS1015 ads0(0x49);



void I2C_setup(){ 
    Serial.begin(115200);
    Wire.begin(PIN_SDA,PIN_SCL);  
    ads0.setGain(GAIN_TWO);// 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
    ads0.begin();
    
}


void hardwareInit()
{
    /*
    Wire.begin(PIN_SDA,PIN_SCL);
    ads0.setGain(GAIN_TWO);// 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
    ads0.begin();*/
    I2C_setup();
    for (int i = 0; i < NUM_DISCRETE_INPUT; i++)
    {
        pinMode(pinMask_DIN[i], INPUT);
    }
    
    for (int i = 0; i < NUM_ANALOG_INPUT; i++)
    {
        pinMode(pinMask_AIN[i], INPUT);
    }
    
    for (int i = 0; i < NUM_DISCRETE_OUTPUT; i++)
    {
        pinMode(pinMask_DOUT[i], OUTPUT);
    }

    for (int i = 0; i < NUM_ANALOG_OUTPUT; i++)
    {
        pinMode(pinMask_AOUT[i], OUTPUT);
    }
   
}

void updateInputBuffers()
{
    
    for (int i = 0; i < NUM_DISCRETE_INPUT; i++)
    {
        if (bool_input[i/8][i%8] != NULL) 
            *bool_input[i/8][i%8] = digitalRead(pinMask_DIN[i]);
    }
 /* 
   for (int i = 0; i < NUM_ANALOG_INPUT; i++)
    {
        if (int_input[i] != NULL)
            *int_input[i] = digitalRead(pinMask_AIN[i]);
    }*/

    int16_t adc0, adc1, adc2, adc3;
    adc0 = ads0.readADC_SingleEnded(0);
    adc1 = ads0.readADC_SingleEnded(1);
    adc2 = ads0.readADC_SingleEnded(2);
    adc3 = ads0.readADC_SingleEnded(3);

    *int_input[0] = ads0.readADC_SingleEnded(0) * 0.0625 * 5.7;
    *int_input[1] = ads0.readADC_SingleEnded(1) * 0.0625 * 5.7;
    *int_input[2] = ads0.readADC_SingleEnded(2) * 0.0625 * 5.7;
    *int_input[3] = ads0.readADC_SingleEnded(3) * 0.0625 * 5.7;
}
void updateOutputBuffers()
{
    for (int i = 0; i < NUM_DISCRETE_OUTPUT; i++)
    {
        if (bool_output[i/8][i%8] != NULL) 
            digitalWrite(pinMask_DOUT[i], *bool_output[i/8][i%8]);
    }
    for (int i = 0; i < NUM_ANALOG_OUTPUT; i++)
    {
        if (int_output[i] != NULL) 
            digitalWrite(pinMask_AOUT[i], (*int_output[i] / 256));
    }
}
 
Reply 0 0
Reply