Arduino Example

Arduino Analog Threshold Example

Turn output on when analog value crosses a threshold.

Wiring

Check pinout, power supply, common GND and voltage levels before connecting the module.

Full code

const int sensorPin = A0;
const int ledPin = 13;
const int threshold = 500;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int value = analogRead(sensorPin);
  digitalWrite(ledPin, value > threshold ? HIGH : LOW);
  Serial.println(value);
  delay(100);
}

How it works

This lesson matches the page title and gives a clean starting point for this exact example.

Common mistakes

Check board selection, wiring, libraries, power supply and serial monitor baud rate.

Frequently asked questions

Can I copy this code?

Yes. Adapt pins, credentials and libraries for your board.

Why does it not compile?

Install required libraries and select the correct board.