Menu
CoddyTech

Light Sensor Switch Pin

Part of the Inputs section of Coddy's Arduino journey. Lesson 14 of 22.

The light module has a small chip of its own, a comparator, that does the threshold for you. It compares the light with a level set by the little screw on a real module and drives the DO pin: LOW when it is bright, HIGH when it is dark. On this board the level is half light.

DO is a digital signal, so you read it like a button, with digitalRead(). The module drives the pin itself, so it needs no pull-up: a plain INPUT is right.

pinMode(7, INPUT);

if (digitalRead(7) == HIGH) {
  // dark
}

To print a message only when the light changes, use the edge idea from the button lessons: compare the reading with the one from last time.

challenge icon

Challenge

Easy

The module's DO is on pin 7 and the LED on pin 13:

  • set pin 7 to INPUT
  • light the LED while DO is HIGH (dark), otherwise turn it off
  • when the reading changes, print Dark or Bright

Press Run, move the light slider past the middle both ways, then Check.

Try it yourself

const int sensorPin = 7;
const int ledPin = 13;

int lastDark = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  // set the sensor pin to INPUT
}

void loop() {
  // read DO, light the LED in the dark
  // print Dark or Bright when the reading changes
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Inputs

Practice on your own: Arduino playground