Embedded Systems Report Lab 3
Embedded Systems Report Lab 3
1
Table of Contents
Introduction..................................................................................................................................3
Parts that we will need..................................................................................................................3
BreadBoard Layout........................................................................................................................4
Theory behind the push button......................................................................................................4
Push button control simulation using proteus 8..............................................................................6
Arduino sketch code......................................................................................................................6
Results...........................................................................................................................................7
Code explanation...........................................................................................................................8
PushButton using toggle switch.....................................................................................................9
Problem.....................................................................................................................................9
Parts we will need......................................................................................................................9
BreadBoard Layout..................................................................................................................10
The Code..................................................................................................................................10
Code explanation.....................................................................................................................11
2
Introduction
In this lab, we will learn how to control a LED by pushing a button.We have learned in earlier
tutorials. how to automatically blink an LED with Arduino. That program will run as long as
there is a power for the Arduino. Let’s go a bit further and try to control the LED with a button.
We use a pushbutton to control the Blinking. In other words, we are going to make a simple
program which allows us to control the LED by pushing a button.
Dupont Wires ×7
LED ×1
10K Resistor ×1
3
BreadBoard Layout
We will use one resistor for the LED and one for the Pushbutton.
When the pushbutton is open (unpressed) there is no connection between the two legs of the
pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a
LOW. When the button is closed (pressed), it makes a connection between its two legs,
connecting the pin to 5 volts, so that we read a HIGH.
Here is an animation that explains what happens when those 2 legs meet and separate:
4
Push button control simulation using proteus 8
5
Arduino sketch code
//sketch pushbutton
int buttonPin = 4; // the number of the pushbutton pin
int ledPin = 13; // the number of the LED pin
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (digitalRead(buttonPin) == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Results
6
Code explanation
This code is very simple, I added a variable for the button and attached it to pin 4. Furthermore,
we declared a constant variable for the LED and attached it to pin 13.
We need to set the button variable to an input mode and set the LED to an output mode.
In the void loop () part we are going to control the LED by using an If statement.
The If statement above will check the following condition and checks if it is true. We use a
comparison operator “==”. If the button is pressed it sends a HIGH signal to the Arduino.
Therefore, we need to check if the condition is met that the buttonPin is HIGH. If that condition
is not met the loop goes to the Else Statment where the LED is turned off.
If you uploaded the sketch you will see that the LED is turned on every time you push the button.
But what if we want that the LED will stay on if the button is pressed and turned off when the
button is pressed again? For that, we will have use to button as a switch and determine the state
in which it last was. That is what we will explore in the next lab.
7
PushButton using toggle switch
Problem
The problem arises that if the button is unpressed the LED turns off. If you would like to keep
the LED on after the button is pressed we need to remember the state of the button. We can use
the if function for this. We will use and modify the code we already made
Dupont Wires ×7
LED ×1
10K Resistor ×1
8
BreadBoard Layout
The Code
// Make a Toggle Switch Button
// Define the pins being used
int pinLed = 13;
int pinSwitch = 4;
// declaring variables to hold the new and old switch states
oldSwitchState = LOW;
newSwitchState = LOW;
LEDstatus = LOW;
int WaitTime=100;
void setup()
{
pinMode(pinLed, OUTPUT);
digitalWrite(pinLed, LOW);
pinMode(pinSwitch, INPUT);
}
void loop()
{
newSwitchState = digitalRead(pinSwitch);
if ( newSwitchState != oldSwitchState )
{
// has the button switch been closed?
if ( newSwitchState == HIGH )
{
if ( LEDstatus == LOW ) {
digitalWrite(pinLed, HIGH);
LEDstatus = HIGH;
}
else {
digitalWrite(pinLed, LOW);
LEDstatus = LOW; }
}
oldSwitchState = newSwitchState;
delay(WaitTime); // debouncing
}
9
}
Code explanation
After we declared our pins and our variables we arrive at the void loop() part of our sketch. In
the loop function, the sketch checks whether or not the button is pressed. The sketch will read the
signal from the button to give the newSwitchState a LOW or HIGH value. HIGH for pressing the
button and LOW if the button is not pressed. We need to know a couple of things to make sure
that the button works as a switch. Firstly, we need to check the condition whether
the newSwitchState is different from the oldSwitchState. For that, we use a comparison operator
“!=”. Rember, if the button is pressed it sends a HIGH signal if it is not pressed it sends a LOW
signal.
After the check is completed we need to check if the button is sending a HIGH signal meaning
that the button is pressed. Furthermore, we need to know whether the LEDstatus is HIGH or
LOW. In other words, we need to know whether the LED is on or off. If the LEDstatus is LOW
and the button is pressed then the program should recognize that we want to turn the LED on.
The last statement that we will use is an Else statement. If the if statements are false, and the
button is pressed, we want the LED to turn off.
An important part of the sketch is to set the oldSwitchState to the current newSwitchState. We
will add the below code to change the state of the button. This is is important, since we will use it
in our if statements.
And finally we use the delay to avoid the problem of switch bouncing, if the pushButton is pressed there
will be no threshold to be set to ‘’0’’.
Proteus Simulation
Before Pressing
10
After Pressing
11