Sample potentiometers, light sensors, thermistors, and analog robot signals.
Open in compiler →/*
Roboda verified Arduino sample 0503
Feature: Analog Sensor Read
Generated for online build, classroom practice, and hardware upload testing.
*/
const byte sensorPin = A2;
const int threshold = 540;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
int value = analogRead(sensorPin);
digitalWrite(13, value > threshold ? HIGH : LOW);
Serial.print("analog=");
Serial.println(value);
delay(360);
}
How it works
Read a variable analog voltage and compare it with a threshold.
- Connect the sensor output to the listed analog pin.
- Connect sensor VCC and GND to the board.
- Open Serial Monitor to see raw 0-1023 values on AVR boards.
- Adjust threshold for your sensor range.
Connections
| Pin | Connect to | Note |
|---|---|---|
| A2 | Potentiometer wiper, LDR divider, thermistor divider, or analog sensor output | Use the same board ground as the sensor. |
| D13 | Built-in LED | Turns on when the analog value crosses the threshold. |