ReadAnalogVoltage and then do something

Hi everyone, I'm very new to programming. I understand some basics, but it really just makes my head hurt.

I have a project for a friend that wanted to remove the speedometer from his motorcycle (which has lights for low fuel) but he still wants the low fuel light.

There's a signal wire coming from the tank that sends voltage from 12V DC down to I guess 0V DC when the tank is fully empty.

I know that when the tank is approximately 1/4 full, the voltage coming from the tank sensor will be about 5V DC. So, basically, I just want to monitor the voltage and when it gets to 5V or below, I want it to turn an LED on. Some extra programming to maybe check it 10 times in a 15 second period to make sure that it stays at 5V or below, so I don't get false readings if fuel is sloshing around, causing the light to flash on and off a lot.

I have an Arduino UNO from Sainsmart that has been sitting in my office for years, because I wanted to learn this stuff and tried a few experiments and thought it was cool and all, but didn't really have a use for it, until now.

It's overkill for the project, I'm well aware, but I want to see if I can get it to work anyway. So, I've googled around and found this post: Arduino Forum

Which is sort of what I'm looking for, but not quite.

I really really don't understand where the 1023 value comes from in this and I have lots of questions:

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

But, let's say that I need to plug in a wire into an analog PIN, and that voltage may be as high as 13.8V DC possibly. What will the number be?

Do I set it to?:

float voltage = sensorValue * (14.0 / 1023.0);

Can I plug something into the board that's higher than 5V, or will it fry it?

Can anyone help me with some code to make this happen? I thought it would be really easy but I'm finding that I'm really bad at this stuff. So go easy on me.

You don't place 12V on an Arduino input, never!

You reduce the voltage first using a voltage divider so it's 5V max. That you can connect to the Arduino's input.

The analog input uses a 10 bit analogue-to-digital converter. 10 bits can hold a value from 0 to 1023.

You can debate if 1023 should actually be 1024 as there are 1024 steps. I think it should be 1024, others will say that you should use 1024.

Thanks for telling me that it won't accept 12V prior to me trying and blowing up my board! lol.

A voltage divider eh? Don't think I got one of them in my kit. I don't suppose a resistor would work instead?
EDIT: Duh, I just looked this up and yeah, I have a potentiometer that could be used.

So, basically you're telling me that the signal is going to have to be 5v or less, I'd have to step the voltage of my source down, figure out at which point I'd be hitting 5v and correlate that to the stepped down voltage (be it 3v or whatever) and then from there, if that point is reached, I can then make an actionable function to turn on a LED, right?

sterretje:
The analog input uses a 10 bit analogue-to-digital converter. 10 bits can hold a value from 0 to 1023.

You can debate if 1023 should actually be 1024 as there are 1024 steps. I think it should be 1024, others will say that you should use 1024.

Thanks for the explanation! I had no idea where the 1023 (1024) came from, but that makes total sense now

A voltage divider is 2 resistors. Check Voltage Dividers - learn.sparkfun.com

Steve

I don't suppose a resistor would work instead?

No, but 2 resistors will work.

A voltage divider tutorial. (same as slipstick linked)

A voltage divider calculator.

@OP

1. The following setup of Fig-1 and the associated codes could be helpful.
adc328py.png
Figure-1:

float V = 0;

void setup()
{
  Serial.begin(9600);
  analogReference(DEFAULT);  //5V Full Scale (FS) for ADC; weight for LSBit of ADC = 5/1023 = 4.9 mV
  pinMode(13, OUTPUT);
}

void loop()
{
  for (int i = 0; i < 10; i++)
  {
    V += (float)(5.0 / 1023.0) * analogRead(A0);
    delay(1000);
  }
  V = V / 10.0;  //average of 10 samples taken at 1-sec interval
  if (V <= 1.56)  //floating point number is to compared by inequality as float numbers are never exact
  {
    Serial.println("Low Fuel Tank.");
    digitalWrite(13, HIGH);  //LED ON
    V = 0.0;  //reset for next measurement
  }
  else
  {
    Serial.println("Full Fuel Tank.");
    digitalWrite(13, LOW);  //LED OFF
    V = 0.0; //reset for next measurement
  }
}

2. The following diagram of Fig-2 may help you to get some internal details of the ADC Module of the ATmega328P MCU of the UNO Board.


Figure-2:

adc328py.png

GolamMostafa:
@OP

1. The following setup of Fig-1 and the associated codes could be helpful.
adc328py.png
Figure-1:

float V = 0;

void setup()
{
 Serial.begin(9600);
 analogReference(DEFAULT);  //5V Full Scale (FS) for ADC; weight for LSBit of ADC = 5/1023 = 4.9 mV
 pinMode(13, OUTPUT);
}

void loop()
{
 for (int i = 0; i < 10; i++)
 {
   V += (float)(5.0 / 1023.0) * analogRead(A0);
   delay(1000);
 }
 V = V / 10.0;  //average of 10 samples taken at 1-sec interval
 if (V <= 1.56)  //floating point number is to compared by inequality as float numbers are never exact
 {
   Serial.println("Low Fuel Tank.");
   digitalWrite(13, HIGH);  //LED ON
   V = 0.0;  //reset for next measurement
 }
 else
 {
   Serial.println("Full Fuel Tank.");
   digitalWrite(13, LOW);  //LED OFF
   V = 0.0; //reset for next measurement
 }
}




**2.** The following diagram of Fig-2 may help you to get some internal details of the ADC Module of the ATmega328P MCU of the UNO Board.
![adc328p.png|667x546](upload://3A6l1urPaCrsDypL9YzojgEGC5a.png)
Figure-2:

Thank you for this! It's much appreciated. I'm going to try to get something built this weekend and see if I can get it working.

GolamMostafa:
@OP

1. The following setup of Fig-1 and the associated codes could be helpful.
adc328py.png
Figure-1:

float V = 0;

void setup()
{
 Serial.begin(9600);
 analogReference(DEFAULT);  //5V Full Scale (FS) for ADC; weight for LSBit of ADC = 5/1023 = 4.9 mV
 pinMode(13, OUTPUT);
}

void loop()
{
 for (int i = 0; i < 10; i++)
 {
   V += (float)(5.0 / 1023.0) * analogRead(A0);
   delay(1000);
 }
 V = V / 10.0;  //average of 10 samples taken at 1-sec interval
 if (V <= 1.56)  //floating point number is to compared by inequality as float numbers are never exact
 {
   Serial.println("Low Fuel Tank.");
   digitalWrite(13, HIGH);  //LED ON
   V = 0.0;  //reset for next measurement
 }
 else
 {
   Serial.println("Full Fuel Tank.");
   digitalWrite(13, LOW);  //LED OFF
   V = 0.0; //reset for next measurement
 }
}




**2.** The following diagram of Fig-2 may help you to get some internal details of the ADC Module of the ATmega328P MCU of the UNO Board.
![adc328p.png|667x546](upload://3A6l1urPaCrsDypL9YzojgEGC5a.png)
Figure-2:

Be mindful not to fry the Arduino with a large current. So, you need to carefully choose the resistors for your voltage divider. Choose reasonably high resistor values.

nandgeek:
Be mindful not to fry the Arduino with a large current. So, you need to carefully choose the resistors for your voltage divider. Choose reasonably high resistor values.

An Arduino input will only draw what is needed. So there will not be a large current going into the Arduino.

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