Switch Case Statement Arduino - Analog Read Serial and Switch Case With Sensor Input Light Dependent Resistor

Switch Case Statement Arduino – Analog Read Serial and Switch Case With Sensor Input Light Dependent Resistor

Switch Case Statement Arduino – Analog Read Serial and Switch Case With Sensor Input Light Dependent Resistor

Like if statements, switch case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run.

The break keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions (“falling-through”) until a break, or the end of the switch statement is reached.

An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements, or you can use the switch statement. Switch allows you to choose between several discrete options. This tutorial shows you how to use it to switch between four desired states of a photo resistor: really dark, dim, medium, and bright.

This program first reads the photoresistor. Then it uses the map() function to map its output to one of four values: 0, 1, 2, or 3. Finally, it uses the switch() statement to print one of four messages back to the computer depending on which of the four values is returned.

Hardware Required

  • Arduino Board
  • photoresistor, or another analog sensor
  • 10k ohm resistors
  • hook-up wires
  • breadboard


The photoresistor is connected to analog in pin 0 using a voltage divider circuit. A 10K ohm resistor makes up the other side of the voltage divider, running from Analog in 0 to ground. The analogRead() function returns a range of about 0 to 600 from this circuit in a reasonably lit indoor space.

There are two steps required in the coding part of this experiment:

  1. AnalogReadSerial – to determine the maximum ADC values of LDR
  2. The SwitchCase Statement for creating the specific range of cases according o the input detected by sensor

The Code (AnalogRead Serial) – Available in the Example > Basic > AnalogReadSerial

Experiment 1: AnalogReadSerial


  This example code is in the public domain.


// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  delay(1);        // delay in between reads for stability

Experiment 2: SwitchCase Statement 

  Switch statement

  Demonstrates the use of a switch statement. The switch statement allows you
  to choose from among a set of discrete values of a variable. It's like a
  series of if statements.

  Source: https://www.arduino.cc/en/Tutorial/BuiltInExamples/SwitchCase

// these constants won't change. They are the lowest and highest readings you
// get from your sensor:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 887;    // sensor maximum, discovered through experiment

void setup() {
  // initialize serial communication:

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the range value:
  switch (range) {
    case 0:    // your hand is on the sensor
      Serial.println("dark" + String (" ") + String(sensorReading));
    case 1:    // your hand is close to the sensor
      Serial.println("dim" + String (" ") + String(sensorReading));
    case 2:    // your hand is a few inches from the sensor
      Serial.println("medium" + String (" ") + String(sensorReading));
    case 3:    // your hand is nowhere near the sensor
      Serial.println("bright" + String (" ") + String(sensorReading));
  delay(500);        // delay in between reads for stability
No Responses

Leave a Reply

Your email address will not be published.