Home > Projects > How To – DIY – Projects > Switch Statement used with serial input using Arduino

Switch Statement used with serial input using Arduino

Summary of Switch Statement used with serial input using Arduino


This tutorial demonstrates using a switch statement on an Arduino to select between multiple discrete options. It reads serial input characters and lights one of five LEDs (pins 2–6) for characters a, b, c, d, or e; any other character turns all LEDs off. The circuit uses five LEDs in series with 220 ohm resistors connected to digital pins 2–6. The sketch runs while connected to a computer; open the Serial Monitor and send characters to control the LEDs.

Parts used in the Switch Statement LED Project:

  • Arduino board
  • 5 LEDs
  • 5 220 ohm resistors
  • Breadboard
  • Hook-up wire

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.

Switch Statement used with serial input using Arduino

This tutorial shows you how to use switch to turn on one of several different LEDs based on a byte of data received serially. The sketch listens for serial input, and turns on a different LED for the characters a, b, c, d, or e.

Circuit

Five LEDs are attached to digital pins 2, 3, 4, 5, and 6 in series through 220 ohm resistors.

To make this sketch work, your Arduino must be connected to your computer. Open the Serial Monitor, and send the characters a, b, c, d, or e, or anything else.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

Switch Statement used with serial input using Arduino schematic

Code

/*
Switch statement  with serial input
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.
To see this sketch in action, open the Serial monitor and send any character.
The characters a, b, c, d, and e, will turn on LEDs.  Any other character will turn
the LEDs off.
The circuit:
* 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
created 1 Jul 2009
by Tom Igoe
Major Components in Project

Hardware Required

  • Arduino Board
  • (5) LEDs
  • (5) 220 ohm resistors
  • breadboard
  • hook-up wire

Quick Solutions to Questions related to Switch Statement LED Project:

  • What does the sketch do?
    The sketch reads serial characters and turns on one of five LEDs for characters a, b, c, d, or e, and turns all LEDs off for any other character.
  • Which Arduino pins are used for the LEDs?
    The LEDs are attached to digital pins 2, 3, 4, 5, and 6.
  • What component values are used with the LEDs?
    Each LED is used with a 220 ohm resistor.
  • How are the LEDs connected in the circuit?
    Each LED is connected to its digital pin through a 220 ohm resistor; the tutorial describes five LEDs attached to pins 2 through 6.
  • How do you control which LED lights?
    Open the Serial Monitor and send characters a, b, c, d, or e to light the corresponding LED; sending any other character turns LEDs off.
  • Does the sketch require a computer connection?
    Yes, the Arduino must be connected to the computer so you can use the Serial Monitor to send characters.
  • Can other characters besides a–e be used to light LEDs?
    No; only a, b, c, d, and e turn on LEDs. Any other character turns the LEDs off.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top