Problem in printing the values

I'm making this Electric Impedance Tomography project on proteus and the code is in arduino Mega1280. The circuit is working fine. The problem is when I'm simulating the circuit on proteus, the input voltages coming to A0 pin are being printed as 0 for some reason. Even though when I put a probe on that wire and it shows the voltage, arduino is printing as zero. Can someone help me?

Here is the code:

//library of the multiplexors
#include <CD74HC4067.h>
//library for calculating RMSvalues
#include <TrueRMS.h>
// loop period time in microseconds (time between samples)
#define LPERIOD 1000
// define the used ADC input channel
#define ADC_INPUT 0

// RMS window of 40 samples, means 2 periods at 50Hz
#define RMS_WINDOW 40
unsigned long nextLoop;
int adcVal;
int cnt=0;
// ADC full scale peak-to-peak is 5.00Volts
float VoltRange = 5.00;
// create an instance
Rms readRms ;
CD74HC4067 my_mux(22,23,24,25);
CD74HC4067 my_mux2(26,27,28,29);
CD74HC4067 my_mux3(30,31,32,33);
CD74HC4067 my_mux4(34,35,36,37);
void setup()
{
 
pinMode(A0, INPUT);
Serial.begin(9600);
Serial.println("LABEL,v");
Serial.print("DATA, ");
readRms.begin(VoltRange, RMS_WINDOW, ADC_10BIT, BLR_ON,CNT_SCAN);
readRms.start();
// Set the loop timer variable for the next loop interval.
//micros( ) Returns the number of microseconds since the Arduino board began running the current program.

nextLoop = micros() + LPERIOD;

//electrode 0 and 1//electrode 0 and 1
my_mux.channel(0);
my_mux2.channel(1);
for (int k=2;k<15;k++)
{
my_mux3.channel(k);
my_mux4.channel(k+1);
for(int i=0;i<500;i++)
{
// Read the ADC and remove the DC-offset
adcVal = analogRead(ADC_INPUT);
//take an instance again
readRms.update(adcVal);
cnt++;
//repeating the readings of one sample
if(cnt >= 500)
{
// publish every 0.5s
readRms.publish();
Serial.print(readRms.rmsVal,2);
Serial.println();
Serial.print("DATA, ");
cnt=0;
}
// wait until the end of the time interval
while(nextLoop > micros());
// set next loop time to current time + LOOP_PERIOD
nextLoop += LPERIOD;
}
}
}
void loop(){
}

Here is the link to the proteus file:
https://www.mediafire.com/file/7bj7qdcurz15vl5/Phantom2.pdsprj/file

What do you see if you print adcVal after

      adcVal = analogRead(ADC_INPUT);

there are mutliple things that I see:

  1. inside the arduino IDE you should press Ctrl-T to autoformatting your code
    The indentions are important to the reader of the code to easiyl see which command is inside which loop

  2. The way you are calculating

      nextLoop += LPERIOD;

      while (nextLoop > micros());

does not handle a roll-over of micros() properly

  1. all your code is inside function setup which means
    the code runs through one single time and then the microcontroller is executing the empty
void loop() {
}
  1. configuring the serial interface for 9600 baud means for printing something like

DATA, 10

which are 8 bytes each byte 10 bits (1 startbit 8 databits 1 stopbit)
8 * 10 * 1 / 9600 = 0,0083 seconds = 8,33 milliseconds

where you tried to read once every 1,0 milliseconds does not work.

best regards Stefan

It should be printing the voltage values but this is all it's printing


All zeroes

Hey Stefan!
Im sorry I provided the code the way I did
Im relatively new to coding so I just copy pasted the code from a paper I found. And also the code provided didn't have any loop function. I just added an empty loop function so that the code can be compiled successfully.
Can you explain me how I can fix the issue?
It should be printing the voltage values but this is all it's printing

should be

#define ADC_INPUT A0

and don't configure A0 as INPUT

Hey @gcjr !!
I changed it to A0 and removed the line where I declared it as input but still the same issue. It's printing all zeros

are you actually printing the value read from the pin, as UKHeliBob suggested?

    adcVal = analogRead(ADC_INPUT);
    Serial.println (adcVal);

Oh okay I tried that too. To print adcVal. This is what I got

It's printing the same values over and over even though the values on the voltage probe of that wire are varying

so the value is not longer zero?

how quickly is the voltage changing and how quickly is the input being read?

Yeha the voltage is no longer zero. I'm not sure how fast it exactly is but the voltage variation is rapid on the probe. By the way I provided the proteus file. Can you please check it if possible? I'm not very good at debugging and coding so Im assuming you might get a better idea if you check it

have no clue what proteus is.

why aren't you putting code under loop() instead of performing some limited number of operations?

why not read/print the input every 100 ms?

Tbh I copy pasted it from a research paper. The original code didn't have any loop function. I added an empty void loop just to avoid compilation error. And for this situation I just want 13 values so I didn't modify to put it in loop

not sure what to tell you.

however, just noticed that the values being printed are the max possible value, suggesting the input exceed the supply voltage on the board. do you need a voltage divder to reduce the voltage?

you have mainly two ways to make this work

way 1:
play the lottery not knowing if you "win" = solve the problem after 50 minutes or 50 years.

way 2: learning the basics how programming works.
if you do not yet understand what an advice like

means
your knowledge about programming is way too less to get this to work.
Do yourself a favor and take at least some hours to learn the very basics of programming.

One way among a lot of others to do this is
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

There is nothing at all wrong with using

#define ADC_INPUT 0

This is a red herring.

isn't the correct value for A0, 14?

#define PIN_A0   (14)
#define PIN_A1   (15)
#define PIN_A2   (16)
#define PIN_A3   (17)
#define PIN_A4   (18)
#define PIN_A5   (19)
#define PIN_A6   (20)
#define PIN_A7   (21)

static const uint8_t A0 = PIN_A0;

The analogRead() function makes the assumption that the function parameter refers to an analog pin, so you can use just the number 0 for A0, 1 for A1, etc. The danger with this is that pinMode(), digitalWrite(), and digitalRead() cannot make this assumption, and will instead use digital pin 0 when given the 0 instead of A0. If the code had used pinMode(ADC_INPUT) it would have set the mode of digital pin 0 (the Rx pin) instead of A0.

1 Like

I think I see your problem. The input is always 1023 (5V) and you have 'baseline restoration' turned on. I think that means the RMS library is taking an average of your readings and calling that the baseline (0V) . Since your signal is not varying you never get a signal other than baseline. The RMS average is 0.

  • blr sets the automatically baseline restoration function on or off. Use the predifined constants BLR_ON or BLR_OFF.

hi all,
sorry for me joining to this thread, bit i'm just dealing with this

my question is

what are the braces around the number at
#define PIN_A3 (17)
meaning ?

would
#define PIN_A3 17
work different ?

from standard C and other languages (i.e. Java) it's not common to me
to write braces here

thanks for advices