Professional Documents
Culture Documents
Portfolio Assignments Assignment 1 Push-Button Inputs & Output LED's
Portfolio Assignments Assignment 1 Push-Button Inputs & Output LED's
TECHNOLOGY
Microcontroller Systems IV
MSC411
Portfolio Assignments
Assignment 1
Push-button inputs & Output LED’s
Initials: KS
Surname: Buthelezi
Student Number: 218056413
Cell Number: 0832126951
Email: katlehobuthelezi@ymail.com
List of Figures
2
Assignment 1: Playing with push-button inputs and output LED’s
1. Keywords
2. Introduction
The purpose of the assignment is to demonstrate the operation of push-button inputs and
output LED’s interfaced with a microcontroller. In this project we have chosen to use the
Arduino Mega board to write a program in the C language to fulfill the requirements of the
project. Arduino is an open source platform for students, hobbyists and professionals that want
to create devices that interact with their environment using sensors and actuators. It is also
capable of acting as minicomputer by taking inputs and controlling the outputs for various
electronic devices. The Arduino board is easily programmable and can be reprogrammed
effortlessly through its software known as IDE (Integrated Development Environment). The
choice of using an Arduino Board was solely based on the inexpensive costs of the
development board and other components as well as the open source community that allows
for sharing of information.
This board uses an 8-bit ATmega 2560 microcontroller developed by Atmel, which can be
programmed in C or C++ through the Arduino IDE software. The ATmega 2560 has 256K
bytes of In-system Programmable Flash memory (Atmel, 2019) which is more than enough to
run a simple code to control Push Buttons and Output LED’s. This board is designed for more
complex projects with 54 digital I/O pins and16 Analog inputs (Arduino, 2019).
The Arduino Mega board will be used together with the Push button inputs, Output LED’s and
buzzer to achieve the operation described in the sections to follow. A software program will
be coded using the C language and loaded onto the ATmega2560 microchip onboard the
Arduino board to execute the operations of the project.
which are able to increment to the right and decrement to the left when triggered by push-
buttons. This will be done in conjunction with a buzzer that is also triggered with each Push
button. The length of the buzzer tone will be defined in the code.
3. Objectives
The main objective of this design project is to familiarize the reader with the Arduino
development environment and interfacing various input/output devices with the development
board.
3
The project will be carried out by completing the following:
The operation described can be seen in the simplified schematic shown in Figure 1 and 2.
4
4. Hardware
Below is a list of components that were used in the design of this project, each of the main
components will be discussed in the sections to follow.
Arduino Mega2560
3 x LED’s
3 x Push-Buttons
3 x 10 kΩ resistors
3 x 22 Ω resistors
1 x 100Ω resistors
1 x Buzzer
Breadboard
Jumper wires
Microcontroller:
External Power Supply:
USB Plug:
Internal Programmer:
Reset Button:
Analog Pins:
Digital I/O Pins:
Power and GND Pins:
4.3 Buzzer
5. Software
The program code written for Arduino is known as a sketch. The software used for
developing of such sketches for an Arduino is known as the Arduino IDE, which contains the
following parts in it:
Text Editor: This is where the actual code is written using a simplified version of C++
programming language.
5
Message area: This area displays the error messages or any other message that
arises when compiling the sketch.
Text: The IDE console displays text output by the Arduino including error messages
and other information
Console Toolbar: This toolbar contains different buttons like, Upload, Verify, New,
Save and Serial Monitor.
Now we will discuss the basic programming techniques employed in the Arduino IDE
software. The Arduino sketch mainly consists of two main parts
Void Setup ()
Void Loop ()
1) Void Setup ()
The setup () function is called when a sketch starts. It is generally used to initialize variables,
pin modes, start libraries, etc. (Arduino, 2019). Since this is used for initializing the above
mentioned, it will only run once, after each powerup or reset of the Arduino board. Below is an
example code of how the setup () function is used.
2) Void Loop ()
This is the next important function in the sketch. It consists of the part of the code that needs
to be executed continuously in a loop unlike the part of the code written in the setup. Below
is an example code of how the loop () function is used.
6
6. Procedure
The flowchart shown in Figure 3 describes the operation of the circuit, this was used to
develop the code that was loaded onto the Arduino Mega board.
7
Figure 5 Circuit Connection
7. Code/ Software
/********************************************************************************************************
Program: Push Button Inputs & LEDs
Authour: Katleho Buthelezi
********************************************************************************************************/
byte buttonState1 = 0;
byte lastButtonState1 = 0;
byte buttonState2 = 0;
byte lastButtonState2 = 0;
byte buttonState3 = 0;
byte lastButtonState3 = 0;
8
unsigned long switchMillis;
//*******************************************************************************************************************************
void setup()
{
pinMode(ledRED, OUTPUT); // initialize the LEDRED pin as an output
pinMode(ledBLUE, OUTPUT);
pinMode(ledGREEN, OUTPUT);
pinMode(buzzer, OUTPUT); // Set buzzer - pin 4 as an output
pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
pinMode(ResetPin, INPUT);
noTone(buzzer); // Stop sound
digitalWrite(ledRED, HIGH); // On startup led 1, on
}
//*******************************************************************************************************************************
void loop()
{
Serial.begin(9600);
if (millis() - switchMillis >= 10) //for debounce, check switches every 10 milli seconds
{
switchMillis = millis(); // reset timing
checkSwitches();
}
}
//*******************************************************************************************************************************
void checkSwitches()
{
//*****************************************************************************************************************************
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
if (lastButtonState1 != buttonState1)
{
lastButtonState1 = buttonState1; //update to the new state
// if it is, the buttonState is HIGH/pressed:
if (buttonState1 == HIGH) // button is 1
{
//at reset index = -1
index ++; // increment index
if (index > 2) // if index is greater than 2
{
index = 0; // if index is 0
}
switch (index)
{
case 0:
digitalWrite(ledBLUE, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(ledRED, LOW);
digitalWrite(ledGREEN, LOW);
digitalWrite(buzzer, 0); // Switch pressed, buzzer on
tone(buzzer, 100); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
break;
case 1:
digitalWrite(ledBLUE, LOW);
digitalWrite(ledRED, LOW);
digitalWrite(ledGREEN, HIGH);
9
break;
case 2:
digitalWrite(ledBLUE, LOW);
digitalWrite(ledRED, HIGH);
digitalWrite(ledGREEN, LOW);
} //closing buttonState1
//*******************************************************************************************************************************
buttonState2 = digitalRead(buttonPin2);
if (lastButtonState2 != buttonState2)
{
lastButtonState2 = buttonState2; //update to the new state
switch (index)
{
case 0:
digitalWrite(ledBLUE, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(ledRED, LOW);
digitalWrite(ledGREEN, LOW);
case 1:
digitalWrite(ledBLUE, LOW);
digitalWrite(ledRED, LOW);
digitalWrite(ledGREEN, HIGH);
case 2:
digitalWrite(ledBLUE, LOW);
digitalWrite(ledRED, HIGH);
digitalWrite(ledGREEN, LOW);
10
tone(buzzer, 100); // Send 1KHz sound signal...
delay(1000); // wait for 1 sec
noTone(buzzer); // Stop sound...
break;
}
} //END of if (buttonState2 == HIGH)
//******************************************************************************************************************************
buttonState3 = digitalRead(ResetPin);
if (lastButtonState3 != buttonState3)
{
lastButtonState3 = buttonState3; //update to the new state
if (buttonState3 == HIGH)
{
digitalWrite(buzzer, 0); // Switch pressed, buzzer on
tone(buzzer, 100); // Send 1KHz sound signal...
delay(2000); // Wait for 2 sec
noTone(buzzer);
} // closing buttonState3
} //END of if (lastButtonState3!= buttonState3)
The project demonstrated the use of Push Buttons and LED’s as input and outputs. LED 1
was active on start up. When Push Button 1 was pressed, LED 1 became inactive and LED 2
became active, showing a wrap to the right a single time. When Push Button 1 was pressed
again, LED 2 became inactive and LED 3 became active once again showing a wrap to the
right, a single time. The circuit LED’s behaved similarly when Push button 2 was pressed but
with the LED’s wrapping towards the left.
With each LED increment/decrement the buzzer sounded. When Push button 3 was pressed,
all LED’s switched off and the buzzer sounded, as this button acted as reset button.
Using the C++ language, this operation was achieved and demonstrated using an Arduino
MEGA microcontroller.
9. References
11