Blinking multiple LEDs using switch case

Here, we will discuss a project to turn ON one led from multiple LEDs based on the value in the variable.

Let's start with the project.

Hardware Required

The components required for the project are listed below:

  • 4 x red LED
  • 4 x 220 Ohm Resistors
  • Arduino UNO R3 board
  • Jump wires

We can use any color LED as per our choice.

Principle

We will connect the four LEDs to pins 12, 11, 8, and 6 of the Arduino board. The resistors of 220 Ohm are connected in series with the LEDs.

The switch statement will compare the declared value of a variable with statements in other cases. The code associated with the matching case will run.

We will also change or rearrange the LEDs connected to the specified pin number on the board.

Structure of the project

The structure clearly shows the UNO board's pinout, and the four LEDs with resistors in series are connected to the board.

It is shown below:

Blinking multiple LEDs using switch case

Connection

The connection of the above project is discussed below:

  • Connect the resistor of 220 Ohm in series with the four LEDs. Now connect it to the pin number 12, 11, 8, and 6 of the Arduino board.
  • Connect the negative terminal of the five LEDs to the GND (Ground).

Sketch

Consider the below code:

In the above code, the specified value of variable matches with case 3. Hence, the LED connected to pin number 11 will light up.

Connection Diagram

We will show the connection using the Simulator because the connections become clearer and more precise.

We can make the same connection using the hardware devices.

Blinking multiple LEDs using switch case

Output

The LED connected to the PIN 11 will light up, as shown below:

Blinking multiple LEDs using switch case




Latest Courses