voltage sensor unstable in my code

Hello everyone

I’m trying to use a voltage sensor of 0-25vcc but when I do this using a code taken from the internet it works correctly, but when I write the sensor code in my design sketch it reads once correctly but a 10 sometimes wrongly, I spent a lot of time researching but I did not find anyone who had a similar problem to share a solution, attached the sensor image, any code I get on the internet works but when I do the join with my sketch it reads values strangers.

I thank you all.

(deleted)

So... What answer do you expect?

What voltage are you trying to sense? Appears to be a 5:1 voltagedivider for DC.

I’m sorry spycatcher2k I’m still new here and also with Arduino programming
I attached a file with my code

hello outsider the sensor reads up to 25vcc and it’s great for me this value I’m going to work with something around 12vcc follows a link from the sensor https://www.ebay.com/itm/2pcs-Standard-Voltage-Sensor-Module-For -Robot-Arduino / 261961157472? Hash = item3cfe1a0b60: g: FdQAAOSw-vlVnncO

my code.ino (4.64 KB)

Your math appears correct, put a serial.println on line 149:

Serial.println(tensao_lambda);

See what you’re getting.

I was reading a value of 4.27V so it reads 4.27v and then reads 0.20v several times until read back 4.27v and so sussetively

Sure sounds like a faulty connection, check your wiring carefully and the sensor circuit board with a magnifying glass looking for cold solder joints or broken traces. Flex the board gently, see if that makes a difference, I see E-Bay doesn't list that anymore, sold out? Defective batch?

if I use this code alone it works perfectly so we can say that it is not the right physical part?

vlr_tensao_lambda = analogRead (A0); // port of sensor votage
tensao_lambda = ((vlr_tensao_lambda * 0.00489) * 5); // float to convert A0 result

Well, that seems to eliminate the sensor ,so, somewhere in your code? :confused:

#include <DallasTemperature.h>
#include <OneWire.h>
#include "Nextion.h"
#include <EEPROM.h>
#include <Wire.h>

#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//DeviceAddress sensor1;
NexNumber sensor_cell = NexNumber(0, 2, "n0");
NexNumber sensor_res = NexNumber(0, 6, "n1");
NexNumber sensor_adm = NexNumber(0, 7, "n2");
NexText txt_tensao_lambda = NexText(4, 7, "t0");
NexWaveform wave_temp_cell = NexWaveform(1, 2, "s0");
NexWaveform wave_snr_cell = NexWaveform(0, 5, "s0");
NexPage page5 = NexPage(5, 0, "page5");
NexPage page0 = NexPage(0, 0, "page0");
NexPage page4 = NexPage(4, 0, "page4");
NexGauge gauge_temp  = NexGauge(0, 4, "z0");
NexText txt_temp_cell  = NexText(0, 3, "t1");
NexText txt_temp_fio  = NexText(0, 11, "t2");
NexNumber lambda = NexNumber(4, 2, "lambda");
NexVariable var_lambda = NexVariable(0, 10, "page0.var_lambda");
NexVariable var_ok = NexVariable(0, 11, "page0.var_ok");

const int analog_snr_hho=6; //porta analogica A0
const int analog_snr_res=1;
const int analog_snr_adm=2;
const int ptr_dgl_snr_hho_cell=22;
const int ptr_dgl_snr_hho_res=23;
const int ptr_dgl_snr_hho_adm=24;
const int analog_ptr_tensao_lambda=0;


char buffer_hsensor[10] = {0};
char buffer_tempcell[10] = {0};
char buffer_tempfio[10] = {0};
char buffer_tempadm[10] = {0};
char buffer_tensao_lambda[10] = {0};
int IN1 = 26;
int IN2 = 27;
int pwm=8;
int snr_hho_cell=0;
int snr_hho_res;
int snr_hho_adm;
int vlr_tensao_lambda;
float temp_cell;
float temp_fio;
float tensao_lambda;
//float R1 = 30000.0;
//float R2 = 7500.0;
//float vout;
uint32_t vlr_slr_pwm = 0;
int rele = 25;
int buzzer = 28;
uint32_t vlrfinal2 = 0;
uint32_t vlr_n0=0;
int end_vlr_lambda_eprom=0;
uint32_t vlr_var_lambda=0;
int ler_eprom;

void setup() {
Serial.begin(115200);
Serial1.begin(115200);
Serial2.begin(115200);
//sensors.begin();
pinMode(ptr_dgl_snr_hho_cell, INPUT);
pinMode(ptr_dgl_snr_hho_res, INPUT);
pinMode(ptr_dgl_snr_hho_adm, INPUT);
pinMode(rele, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(pwm, OUTPUT);
//sensors.getAddress(sensor1, 0);
}

void loop()
{
  noTone(buzzer);
  //Serial.println(vlr_tensao_lambda);
 // snr_hho_cell = analogRead(analog_snr_hho); //sensor de vazamento da celula ligado a porta A0
 // snr_hho_res = analogRead(analog_snr_res); //sensor de vazamento do reservatorio ligado a porta A1
 // snr_hho_adm = analogRead(analog_snr_adm); //sensor de vazamento da admissão ligado a porta A2
 //memset(buffer_hsensor, 0, sizeof(buffer_hsensor));
   // itoa(temp, buffer_hsensor, 10);
    //hydrogen.setText(buffer_hsensor);
    //sensor_cell.setValue(snr_hho_cell);
   // sensor_res.setValue(snr_hho_res);
   // sensor_adm.setValue(snr_hho_adm);
    //sensors.requestTemperatures();
//    temp_cell = sensors.getTempCByIndex(0);
//    temp_fio = sensors.getTempCByIndex(1);
    //tempx.setValue(temp_cell);
    memset(buffer_tempfio, 0, sizeof(buffer_tempfio));
    dtostrf(temp_fio,5,2, buffer_tempfio);
    txt_temp_fio.setText(buffer_tempfio);
    
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,HIGH);
    lambda.getValue(&vlr_n0);
    vlrfinal2 = map(vlr_n0, 0, 100, 0, 255);
    var_lambda.getValue(&vlr_var_lambda);
    ler_eprom = EEPROM.read(end_vlr_lambda_eprom);
    
   if (vlr_n0==0)
    {
    lambda.setValue(ler_eprom);
    var_lambda.setValue(ler_eprom);
    analogWrite(pwm, ler_eprom);
    }    
    else
    {
      EEPROM.write(end_vlr_lambda_eprom, vlr_n0);
      analogWrite(pwm, ler_eprom);
    }
    
  if (snr_hho_cell >= 200)
   {
    page5.show();
    digitalWrite(rele, HIGH);
    tone(buzzer, 200);
    digitalWrite(buzzer, HIGH);
    delay(500);
    digitalWrite(buzzer, LOW);
    //analogWrite(AOUTpin, 0);
   }
 
  if (temp_cell >= 40.00)
   {
     page5.show();
     digitalWrite(rele, HIGH);
     tone(buzzer, 200);
     digitalWrite(buzzer, HIGH);
     delay(500);
     digitalWrite(buzzer, LOW);
   }
    //wave_temp_cell.addValue(0,snr_hho_cell);
    //wave_snr_cell.addValue(0,snr_hho_cell);
    //gauge_temp.setValue(map(temp_cell, 0,180,0,360));
    memset(buffer_tempcell, 0, sizeof(buffer_tempcell));
    dtostrf(temp_cell,5,2, buffer_tempcell);
    txt_temp_cell.setText(buffer_tempcell);
  //delay(100);

  vlr_tensao_lambda = analogRead(A0); //port of sensor votage
  tensao_lambda=((vlr_tensao_lambda*0.00489)*5); //float to convert A0 result

  memset(buffer_tensao_lambda, 0, sizeof(buffer_tensao_lambda));
  dtostrf(tensao_lambda,5,2, buffer_tensao_lambda);
  txt_tensao_lambda.setText(buffer_tensao_lambda);
  Serial.println(buffer_tensao_lambda);
}

That’s right, something very strange is disturbing the reading of the sensor, I also tested other ports, other wires, another sensor and nothing.

Hi,
Can I suggest you analogRead the sensor at the start of the loop rather than somewhere in the middle.

Thanks.. Tom.. :slight_smile:

Recently there was a thread about analogRead() and reliability. I think it was something with initialization of some prescaler or what else. The sollution was to read twice and discard the first value, like this:

// Other code here...

analogRead(A0); // Initialize ADC, discard this value
vlr_tensao_lambda = analogRead(A0); //port of sensor votage
tensao_lambda=((vlr_tensao_lambda*0.00489)*5); //float to convert A0 result

//More other code here...

Try it and see if it does the trick :slight_smile:

I've been having a quite a similar problem with a Pro-mini. It had to do with the sensor output impedance, (and affected the other analog channels too).

I recommend:

  1. Grounding all the unusued analog channels.
  2. Using, if possible (band-width), a 4.7 kOhm - 100 nF cap RC filter in the working analog channels.
  3. Use sensors with less than 5 kOhm output impedance (Voltage dividers are dangerous if Thevenin equivalent does not comply).
  4. Double check the "analogRead" (two succesive readings will do).

Good luck!

Regards

good morning everyone

I would like to thank the tips that each one has sent, I will do all the procedures that indicated me and then I will tell the results, I also bought some components to have the hand when you need to do tests.

Good morning everyone

Yesterday I did some more tests and discovered important information, I’m trying to read the voltage of a bridge l298n h but I can not, if I connect any external source to my circuit the sensor works but when I try to read the output value of the h bridge it shows zero, I’m forwarding an attached image of my circuit.

Hi,
OPs circuit;


You cannot do that with the motor output of an H-Bridge, you are shorting one motor output terminal to GND through your voltage sensor.

Why do you want to measure the output of the H-bridge?

Tom.... :slight_smile:

Hello Tom

I am sending a value through the pwm port of the bridge and set something around 1v and would like to display this value on a lcd screen, like a multimeter, but when i try to read through the sensor i can not but by my multimeter it works.

(deleted)

hi spycatcher2k

My multimeter works is normaly, it shows the value correctly even when I'm changing the value of pwm