Read a photoresistor with an Arduino

FunduinoPublished on

Helligkeit messen und eine LED bei Dunkelheit schalten

Jetzt wird’s etwas komplizierter. Erst durchatmen und ein neues Getränk holen!

Aufgabe: Eine LED soll leuchten, wenn es dunkel wird – also wenn du den Fotowiderstand abdeckst.

Circuit diagram with a photoresistor as a voltage divider connected to A0 and an LED connected to pin 12 of the Arduino UNO
Circuit diagram: photoresistor and LED with Arduino UNO

You need this

10 kOhm LDR Photoresistor - light sensor, analog 10 kOhm LDR Photoresistor - light sensor, analog 1,07 € *Item no. F23106706About the product — 10 kOhm LDR Photoresistor - light sensor, analog
1602 I2C LCD Module - 2x16 Characters, HD44780, Blue 1602 I2C LCD Module - 2x16 Characters, HD44780, Blue 3,02 € *Item no. FUN-1010568About the product — 1602 I2C LCD Module - 2x16 Characters, HD44780, Blue
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 7 parts together 23,54 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

Read voltage using a voltage divider

The microcontroller is to read how bright it is using a photoresistor. To do this, you use a simple physical principle: When two components are connected one after the other in a circuit (in series), they share the voltage applied across them.

For example: Two identical lamps are connected in series, with a voltage of 6 volts applied. Using a voltmeter, you find that each lamp has only 3 volts across it. If you connect two different lamps - one of them with lower resistance - you measure two different voltages across the lamps, for example 1.5 volts and 4.5 volts.

A photoresistor changes its resistance depending on the light level. You use this effect to determine a value for brightness or darkness based on the voltage across it. To create a voltage divider, connect the photoresistor and another resistor in series and connect them to 5 volts and ground (Ground / GND).

The microcontroller board can measure and process analog signals (voltages). This is done via the board's analog inputs: The input converts the measured voltage into a number you can work with. 0 volts corresponds to the number 0, while the highest measured value, 5 volts, corresponds to the number 1023 (0 to 1023 is 1024 numbers = 10 bits). For example: If a voltage of 2.5 volts is measured, the microcontroller returns the value 512.

The second resistor in the voltage divider should have a similar value to the photoresistor, so for a 10K LDR, around 10 kOhms. The 220 ohms should be used as a series resistor before the LED.

Use the Serial Monitor

The Serial Monitor is an important part of the Arduino software. This tool lets you display numbers or text sent by the microcontroller board on your PC. This is very useful, because a LCD display is not always connected to the microcontroller for you to read values from.

In this sketch, you’ll use the Serial Monitor to display the values read by the photoresistor.

Why is this useful? Suppose the LED should only start lighting up as it gets dark. Then the sketch needs a condition that says: If the photoresistor reading drops below x, the LED should light up. To do this, you need to know what x is when dusk begins.

The solution: Display the reading at the relevant brightness level - for example, at dusk - in the Serial Monitor. With this information, you can then modify the program as follows: If the photoresistor reading drops below x, turn on the LED. Further explanations are provided in the sketches.

Wire and program

Connect the voltage divider to analog input A0 and the LED to pin 12.

Circuit diagram with a photoresistor as a voltage divider connected to A0 and an LED connected to pin 12 of the Arduino UNO
Circuit diagram: photoresistor and LED with Arduino UNO
Program code – Arduino UNO
// Pins für den Fotowiderstand und die LED
int Fotowiderstand = A0;
int LED = 12;
// der Wert für SchwelleDunkelheit
// kann an die gewünschte "Dunkelheit"
// (den Zeitpunkt, an dem die LED leuchten soll)
// angepasst werden
int SchwelleDunkelheit = 400;
void setup()
{
  pinMode(LED, OUTPUT);
  // Seriellen Monitor starten
  Serial.begin(9600);
}
void loop()
{
  // digitaler Wert der gelesenen Spannung am Pin A0
  int SensorWert = analogRead(Fotowiderstand);
  // Die Kommentarzeichen der beiden folgenden Zeilen entfernen,
  // um den digitalen Wert der ausgelesenen Spannung am Pin A0 anzuzeigen
  // und evtl. den Wert für SchwelleDunkelheit anzupassen
  // Serial.print("Ausgelesener Wert: ");
  // Serial.println(SensorWert);
  if (SensorWert < SchwelleDunkelheit)
  {
    // wenn der digitale Wert am Pin A0 unterschritten wird
    // -> LED einschalten
    digitalWrite(LED, HIGH);
  }
  // andernfalls LED ausschalten
  else
  {
    digitalWrite(LED, LOW);
  }
}

Download: program-code-arduino-uno.ino

Board in the Arduino IDEArduino Uno

You don’t need an additional board package for this. The UNO is included in the Arduino IDE by default.

Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.