Internet of Things Lab Manual
ISBN 9788119221745

Highlights

Notes

  

Chapter 2: Switches, LEDs, and More

LED: A light-emitting diode (LED) is a small electronic component that’s a bit like a light bulb, but is more efficient and requires lower voltages to operate.

Switches:

Pushbutton: Pushbutton, is a very simple device: two bits of metal kept apart by a spring, and a plastic cap that when pressed brings the two bits of metal into contact. When the bits of metal are apart, there is no circulation of current in the pushbutton (a bit like when a water valve is closed); when we press it, we make a connection.

Thermostats:

A switch that opens when the temperature reaches a set value

Magnetic switches (also known as “reed relays”):

Has two contacts that come together when they are near a magnet; used by burglar alarms to detect when a window is opened

Carpet switches:

Small mats that you can place under a carpet or a doormat to detect the presence of a human being (or heavy cat)

Tilt switches:

A simple electronic component that contains two contacts and a little metal ball (or a drop of mercury, but I don’t recommend using those) An example of a tilt switch is called a tilt sensor

Program 2.1: Interface external LED with Arduino and write a program to turn ON LED for 1 sec after every 2 seconds.

Components: Arduino Uno, 1 Green LED, 1 Resistor of 100 Ω

Theory:.

digitalWrite():

Description:

Write a HIGH or a LOW value to a digital pin.

If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.

If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin.

If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.

Syntax:

digitalWrite(pin, value)

Parameters:

pin: the Arduino pin number. value: HIGH or LOW.

Implementation:

Step 1: Open tinkercad dashboard by using the following link and click on new:

https://www.tinkercad.com/dashboard

Step 2: Select Circuit

Step 3:

Circuit Diagram:

Step 4: Click on Code button and select text from edit mode drop down list.

Code:

void setup()

{

pinMode(12, OUTPUT);

}

void loop()

{

digitalWrite(12, HIGH);delay(1000);digitalWrite(12, LOW);delay(2000);

}

Output:

Program 2.2: Program to build an Arduino Traffic Light Controller.

Component: Arduino Uno, 3 LEDs (Red, Yellow, Green), 3 Resistors of 100 ohm

Theory: For traffic light controller we have used 3 LEDs and turning ON using some delay

Implementation:

Step 1: Open tinkercad dashboard by using the following link and click on new:

https://www.tinkercad.com/dashboard

Step 2: Select Circuit

Step 3: Circuit Diagram:

Step 4: Go to code section and write the following program:

void setup()

{

pinMode(4, OUTPUT);pinMode(8, OUTPUT);

pinMode(12, OUTPUT);

}

void loop()

{

digitalWrite(4, LOW);digitalWrite(8, LOW);

digitalWrite(12, HIGH);delay(3000);digitalWrite(4, LOW);digitalWrite(8, HIGH);digitalWrite(12, LOW);delay(1000);digitalWrite(4, HIGH);digitalWrite(8, LOW);digitalWrite(12, LOW);delay(2000);

}

Output:

Exercise 2.1: To interface 5 LEDs with Arduino and write a program to blink 5 LEDs, one at a time, in a back-and-forth formation.

Program 2.3: To interface Push button with Arduino and write a program to turn ON LED whenpush button is pressed.

Component: Arduino Uno, 2 Pushbutton, 2 different LEDs, 4 Resistors of 100 Ω

Theory:

Pushbutton: - The pushbutton is a component that connects two points in a circuit when you press it. The example turns on an LED when you press the button.

Resistors: - Resistors are electronic components, which offer resistance against the current flow, or speaking at a deeper level, against the electrons’ flow. Resistors, denoted by R, are passive components, which means that they don’t generate any electricity at all, but rather reduce voltage and current by dissipating power in the form of heat.

The unit of resistance is ohms (Ω) and resistors are usually built using carbon or metal wire.

digitalRead()

Reads the value from a specified digital pin, either HIGH or LOW.

Syntax

digitalRead(pin)

Parameters

pin: the Arduino pin number you want to read

Returns

HIGH or LOW

Implementation:

Step 1: Open tinkercad dashboard by using the following link and click on new: https://www.tinkercad.com/dashboard

Step 2: Select Circuit and click on New

Step 3: Circuit Diagram:

Step 4: Write the following program in code section:

Code:

int button1 =2;int button2 =7;void setup()

{

pinMode(10, OUTPUT);pinMode(13, OUTPUT);pinMode(button1, INPUT);pinMode(button2, INPUT);

}

void loop()

{

int value1 = digitalRead(button1);int value2 = digitalRead(button2);

if(value1 == HIGH)digitalWrite(10, HIGH);else

digitalWrite(10, LOW);

if(value2 == HIGH)digitalWrite(13, HIGH);else

digitalWrite(13, LOW);

}

Output:

Program 2.4: To interface Push button, Speaker/buzzer with Arduino and write a program to turnON LED and generate a note or tone when push button is pressed.

Component: Arduino Uno, 1 Pushbutton, 1 Buzzer, 3 Resistors

Theory:

Buzzer: - A piezo buzzer is pretty sweet. It’s not liked a regular speaker that you might think of. It uses a material that’s piezoelectric, it actually changes shape when you apply electricity to it. By adhering a piezo-electric disc to a thin metal plate, and then applying electricity, we can bend the metal back and forth, which in turn creates noise.

The faster you bend the material, the higher the pitch of the noise that’s produced. This rate is called frequency. Again, the higher the frequency, the higher the pitch of the noise we hear.

tone():- Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones.

Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

It is not possible to generate tones lower than 31Hz.

Syntax

tone(pin, frequency)

tone(pin, frequency, duration)

Parameters

pin: the Arduino pin on which to generate the tone.

frequency: the frequency of the tone in hertz. Allowed data types: unsigned int. duration: the duration of the tone in milliseconds (optional).

noTone(): - Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.

Syntax

noTone(pin)

Parameters

pin: the Arduino pin on which to stop generating the tone

Circuit Diagram:

Code:

int button1 =4;void setup()

{

pinMode(8, OUTPUT);pinMode(12, OUTPUT);pinMode(button1, INPUT);

}

void loop()

{

int buttonStatus1 = digitalRead(button1);if(buttonStatus1 == HIGH)

{

digitalWrite(8, HIGH);tone(12,1000);

}

else

{

digitalWrite(8, LOW);noTone(12);

}

}

Output:

Exercise 2.2: To interface 2 Push buttons, a Speaker with Arduino and write a program to turn ONLED and generate 2 different notes on two button keyboards.

Program 2.5: To interface Seven Segment Display (SSD) with Arduino and write a program to blink SSD.

Component: Arduino Uno, Cathode 7 Segment Display, 7 Resistors of 115 Ω

Theory:

The 7 Segment display: - The 7-segment display, also written as “seven segment display”, consists of seven LEDs (hence its name) arranged in a rectangular fashion as shown. Each of the seven LEDs is called a segment because when illuminated the segment forms part of a numerical digit (both Decimal and Hex) to be displayed. An additional 8th LED is sometimes used within the same package thus allowing the indication of a decimal point, (DP) when two or more 7-segment displays are connected together to display numbers greater than ten.

Each one of the seven LEDs in the display is given a positional segment with one of its connection pins being brought straight out of the rectangular plastic package. These individually LED pins are labelled from a through to g representing each individual LED. The other LED pins are connected together and wired to form a common pin.

Circuit Diagram:

Code:

void setup()

{

pinMode(2, OUTPUT);pinMode(3, OUTPUT);pinMode(4, OUTPUT);pinMode(5, OUTPUT);pinMode(6, OUTPUT);pinMode(7, OUTPUT);pinMode(8, OUTPUT);

}

void loop()

{

digitalWrite(2, HIGH);digitalWrite(3, HIGH);digitalWrite(4, HIGH);digitalWrite(5, HIGH);digitalWrite(6, HIGH);digitalWrite(7, HIGH);digitalWrite(8, HIGH);

delay(500); // Wait for 500 millisecond(s) digitalWrite(2, LOW);

digitalWrite(3, LOW);digitalWrite(4, LOW);digitalWrite(5, LOW);digitalWrite(6, LOW);digitalWrite(7, LOW);digitalWrite(8, LOW);

delay(300); // Wait for 300 millisecond(s)}

Output:

Exercise 3: To interface Seven Segment Display (SSD) with Arduino and write a program to printnumbers from 1 to 4 on SSD.