Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino temperature sensor

I am trying to build a small program with arduino using a temperature-sensor.

I thought I knew how to do it but I'm getting some weird outputs.

Here is my code:

int sensorPin = 0;
void setup()
{
    Serial.begin(9600);
}
void loop()
{
    int reading = analogRead(sensorPin);
    float voltage = reading * 5.0 / 1024;
    float temperatureC = (voltage - 0.5) * 100;
    Serial.print(temperatureC); Serial.print(" degrees C, ");
    Serial.print(voltage); Serial.println(" volts");
    delay(1000);
}

This code gives me the output:

-26.56 degrees C, 0.23 volts
-26.56 degrees C, 0.23 volts
-27.05 degrees C, 0.23 volts
-26.56 degrees C, 0.23 volts
-26.07 degrees C, 0.24 volts
-26.07 degrees C, 0.24 volts

Why is it - in degrees? and Why can I change it to any pin I want and it will still give me a similar output?

like image 432
Green_qaue Avatar asked Aug 12 '26 01:08

Green_qaue


1 Answers

Analog input 0 is not pin 0.

You should use the defined symbols:
A0,A1,...,A7
for analog inputs.

Try

int sensorPin = A0;  

and your program should work.

If you are curious about the actual values, under your Arduino IDE install look in the file
..\hardware\arduino\variants\standard\pins_arduino.h

like image 198
jdr5ca Avatar answered Aug 13 '26 19:08

jdr5ca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!