Hi All,
This is my first post in this community. I need help regarding coding 4 analog inputs for Arduino Uno.
My project is measuring 2 pressure sensor(0-5V), 1 temp sensor(0 - 5V) and current sensing(0 - 5V). Is there any coding where I can start to work on it? Appreciated for the guidance.
Thanks
Mike
Connect them to analog pins then the coding for each is:
value = analogRead(pinNumber)
For more explanation have a look at the AnalogInput example in the IDE.
Steve
So you have 4 Analog inputs, A0 through A3. The arduino uses a 10 bit ADC so each channel has 1023 quantization levels or we can say 0 to 5.0 volts = 0 to 1023 bits.
So you, as slipstick mentions read each channel and scale the channels to your engineering units. Two pressure sensors (0-5V), one temp sensor(0 - 5V) and one current sensor(0 - 5V). In each case you have units of pressure, temperature and current. You don't provide those ranges but we can say 0 to Full Scale = 0 to 5.0 Volts. Just do the math.
Just as an example if I wanted to read voltage from Analog Channel 0.
int sensorValue = analogRead(0);
// Convert the analog reading (which goes from 0 - 1023) to a Voltage (0 - 5.0 Volts):
float voltage = sensorValue * (5.0 / 1023.0) - 0.00 ;
If I wanted more channels I could use sensorValue1, sensorValue2, sensorValue3 ... You get the idea. Write your code and provide at least a drawing.
Ron
Hi Ron,
Wiring as attached.
My code per below.
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
int sensorValue = analogRead(A1);
Serial.println(sensorValue);
delay(1);
int sensorValue = analogRead(A2);
Serial.println(sensorValue);
delay(1);
int sensorValue = analogRead(A3);
Serial.println(sensorValue);
delay(1);
}
Sensor analog.pdf (111 KB)
You can't do that. You can't declare sensorValue = analogRead(A0); and then change it to A1, A2 and A3. What you can do is define sensorValue1 = analogRead(A0); then sensorValue2 = ... What that code will do is return a number between 0 and 1024 for each channel. Then you scale those numbers to engineering units. Before you post code try and load it. The code you posted would not load.
Ron
The problem with that code is it won't compile - you'll get a redeclaration of variable error. Declare sensorValue only once, after that you can continue to use it, and it will work as expected.
I see you have a mix of ratiometric and fixed voltage output sensors. That's tricky.
The ratiometric ones are best read using the default reference (Vcc), which is normally somewhere in the neigbourhood of 5V. It's typically about 4.5V when powered from USB, 4.9-5.2V when powered from a phone charger through the 5V pin (the recommended way), and rather close to 5V thanks to the built-in regulator when powered by a 7-12V input through the barrel jack or Vin. The thing is: it's all ratiometric, so you don't care what the actual value of Vcc really is, all you need to know is the ratio and that's exactly what the ADC reading represents.
In contrast, the voltage output one (the current sensor) is best read against a known voltage - use the 1.1V internal reference for this (does need calibration due to its 10% tolerance), and if needed a voltage divider on the output of the sensor to get its output in range.
To get this done, you have a few options.
- switch the analog reference before reading the channel.
- use an external ADC for the current sensor output, such as the ADS1115, which has a built-in voltage reference, and read the ratiometric sensors with the Arduino.
- connect the 3.3V output to the AREF pin, use that as external reference, and power your sensors with that same 3.3V. With nothing else this voltage will be very stable. You still may need to add a voltage divider to the output of the current sensor.
Of course you can also just read the current sensor with the default reference and hope for the best. It all depends on how accurate you want things to be. Especially in case your current sensor just has to tell you "current" or "no current' as then the inaccuracy in the reading doesn't matter, really.