How to use analog sensors with the Arduino?

  • Dec 26, 2019
click fraud protection

These sensors are generally cheaper.

And really easy to use!

Analog sensor - a sensor which outputs a voltage value, which is the equivalent measured value.

For example, 2 may mean 25 degrees Celsius from the analog temperature sensor.

You can find many different analog sensors: temperature, light, sound, rain, humidity, etc.

Arduino has analog inputs which may be used to read these values.

Use analogRead (pin) method to read the signal from an analog input.

STEP 1: Connect sensor

Some sensors are supplied in the form of modules with a pin which can be connected directly to an analog input Arduino.

Others are resistive sensors that you need to merge with a resistor in order to read its value.

STEP 2: Download the test code

Download the test code in the Arduino programming environment to test your sensor:

int analogPin = 0; // N analog output that is connected to your sensor
int val = 0; // variable to store the read values
void setup () {
Serial.begin (9600); // Setup the serial connection
}
void loop () {
instagram viewer

val = analogRead (analogPin); // read the data from the analog output
Serial.println (val); // Data transmission over a serial link
}

Step 3: Read the value

Use the «Serial Monitor» in the «Tools» menu, to see the sensor readings.

The analog values ​​are in the range from 0 to 1023:

  • 0 is 0 volts on the analog output;
  • 1023 is a 5 volt analog output;

Step 4: Convert the received data

To find evidence in a certain format you need, for example, degree Celsius for temperature, check the table data matching for your sensor and perform the necessary calculations in my code.