Arduino Voltagemeter

Hello!
I have to build a voltmeter with an Arduino and a display with a classmate. Since the measuring range has to go up to 50 volts, we use 3 analog inputs for 50V, 20V and 5V.
The 50V and 20V input have upstream voltage dividers.
We switch the analogue inputs using 3 Mosfets so that we only have one voltage input. Now my question, somehow our code doesn't work and we don't know why, 3.3V and 5V we take from another Arduino shows 3.9V-4.2V and it doesn't seem to switch properly between the mosfets.
Does anyone spot the error in the code?
Thank you in advance and sorry if there are any grammatical errors.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
uint16_t E5 = A0;
uint16_t E20 = A1;
uint16_t E50 = A2;
int Fet5 = 8;
int Fet20 = 9;
int Fet50 = 10;
int messbereich = 0;
void setup() {

  lcd.begin(16, 4);

  pinMode (Fet5, OUTPUT);
  pinMode (Fet20, OUTPUT);
  pinMode (Fet50, OUTPUT);
  Serial.begin(9600);
}
void Spannungsmessung()
{
  float spannung = 0;
  digitalWrite(Fet50, HIGH);
  digitalWrite(Fet20, LOW);
  digitalWrite(Fet5, LOW);

  spannung = analogRead(A2);
  spannung = ((spannung / 1023) * 50) ;

  if (spannung < 20 && spannung > 5) //19.9
  {
    digitalWrite(Fet50, LOW);
    digitalWrite(Fet20, HIGH);
    spannung = analogRead(A1);
    spannung = ((spannung / 1023) * 20);
    messbereich = 20;
  }
 
  if (spannung < 5) //4.99
  {
    digitalWrite(Fet20, LOW);
    digitalWrite(Fet5, HIGH);
    spannung = analogRead(A0);
    spannung = ((spannung / 1023) * 5);
    messbereich = 5;
  }
  
  lcd.setCursor(0, 0);
  lcd.print(spannung, 1);
}
void Display() {
  lcd.setCursor(4, 0);
  lcd.print("Volt");
  lcd.setCursor(0, 1 );
  lcd.print("Messbereich");
  lcd.setCursor(13, 1);
  lcd.print(messbereich);
  lcd.setCursor(15, 1);
  lcd.print("V");
  messbereich=50;
}

void loop() {

  Spannungsmessung();
  Display();
}

Nice job using code tags for your first post!

We will need a circuit schematic to help.

Welcome to the Arduino forum.
That is quite a project. Please show us a schematic so we have a bit of an idea about what you are working with.

If you are using 3 inputs you don't need MOSFETs. Just read all 3 inputs to get and calculate the 3 voltages.

The Arduino reads with reference to its ground so the 3 voltages and the Arduino must share a common ground. (That's the most common reason for incorrect readings,)

I assume you have a multimeter so if you're getting incorrect readings connect the meter to the analog input and the Arduino's ground to check.

And if there is noise or instability in the actual voltage, note that the Arduino takes a "snapshot" of the voltage for one instant each time you read. A multimeter does SOME smoothing/averaging.

Thank you for the answer,
We use the mosfets to automatically switch between the analog inputs, just as an extra so that you just connect the voltage and the arduino shows it, without any further effort.
They also share a common ground.

Thank you!
I tried to upload the schematic, but there always comes a massage that new members can't upload attachments.

I tried to upload the schematic, but there always comes a massage that new members can't upload attachments.

This requires reaching trustlevel 1 which is an easy thing to do
Get to trust level 1 by…

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.