Intro to Arduino – Week 3

Intro to Arduino – Week 3

Notes from this weeks session are reproduced below.  In case there is any problem in viewing you can visit the original notes created by Stuart on



This week we will use Light Dependent Resistors (LDRs) to control the switching off or on of an LED.

This is the type of automatic switching that happens with Security Lights that turn on at Dusk and off at Dawn.

Here is the sketch.

// pin assignments
 int LED = 13;
 int LDR = 0;
 // variables
 int base;
 int threshold = 200 ;
 // declare inputs and outputs
 // and take a baseline reading
 void setup() {
   pinMode(LED, OUTPUT);
   base = analogRead(LDR);
 // read from the analog input connected to the LDR
 // and print the value to the serial port.
 // the delay is only to avoid sending so much data
 // as to make it unreadable.
 void loop() {
   int liteLevel = analogRead(LDR);
   int x = (liteLevel-base);
   if (x < threshold) {
     digitalWrite(LED, HIGH);
   } else {
     digitalWrite(LED, LOW);

And here is the circuit diagram (note the Resistor value is 1K ohm)


LDR Switch

Lastly here is a link to the Circuit and Code on – LDR SWITCH

This is a fantastic website by Autodesk that allows you to create virtual circuits and upload code to a virtual Arduino and test that it works before making it for real.

The website requires a free login that you can create here