You are on page 1of 11

DEPARTMENT OF ELECTRICAL AND ELECTRONIC ENGINEERING

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

Date: 09 April 2019 Signature: ____________


Contents

Assignment 1: Playing with push-button inputs and output LED’s ......................................... 3


1. Introduction .................................................................................................................... 3
2. Components used ........................................................... Error! Bookmark not defined.
3. Procedure ...................................................................................................................... 7
3.1 Flow Chart .............................................................................................................. 7
3.2 Simulation diagram ................................................................................................. 7
4. Code/ Software .............................................................................................................. 8
5. Discussion and conclusion ........................................................................................... 11

List of Figures

Figure 1 Push-Button 1 as a trigger will cause the LED's to increment by 1 .......................... 4


Figure 2 Push-Button 2 as a trigger will cause the LED's to decrement by 1 ......................... 4
Figure 3 Process Flow Chart ................................................................................................. 7
Figure 4 Circuit Connection ................................................................................................... 8

2
Assignment 1: Playing with push-button inputs and output LED’s

1. Keywords

 LED – Light Emitting Diode


 Arduino
 I/O – Input or Output
 IDE – Integrated Development Environment
 Microcontroller
 Software
 Hardware

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:

 Project to be set up in the development environment;


 Port Pins to be initialized as input and output pins;
 De-bounce hardware push-button inputs;
 Perform the basic binary rotate and increment operations;
 Program and debug hardware development board;

The following process and operation will be demonstrated:

 At start-up or after a manual reset only LED 1 must light up.


 When Push-button 1 is pressed the buzzer sounds and the active LED must rotate
right once.
 Push-button 1 must be released, thereafter when pressed again the buzzer sounds,
and the active LED must rotate right once again.
 When Push-button 2 is pressed the buzzer sounds, and the active LED rotates left
once.
 Push-button 2 must be released and thereafter when pressed again the buzzer
sounds, and the active LED rotates left again.
 If no push-button is pressed, then the active LED must remain in its current position.
 The buzzer will sound for a length of time that is defined in the code.
 When Push-button 3 is pressed the buzzer must sound and all LED's must switch off.

The operation described can be seen in the simplified schematic shown in Figure 1 and 2.

Figure 1 Push-Button 1 as a trigger will cause the LED's to increment by 1

Figure 2 Push-Button 2 as a trigger will cause the LED's to decrement by 1

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

4.1 Arduino Mega2560

 Microcontroller:
 External Power Supply:
 USB Plug:
 Internal Programmer:
 Reset Button:
 Analog Pins:
 Digital I/O Pins:
 Power and GND Pins:

Figure 3 Labelled Diagram of Arduino board and IDE

4.2 Push Button

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.

5.1 Features of Arduino IDE

 The project file is saved with the file extension .ino


 Basic features such as Copy, Past & Cut are supported by the IDE platform.
 The basic structure of an Arduino code will have two functions which are explained in
the sections to follow.

5.2 Programming Basics

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

6.1 Flow Chart

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.

Figure 4 Process Flow Chart

6.2 Simulation diagram

Figure 4 below shows the connected circuit:

7
Figure 5 Circuit Connection

7. Code/ Software

/********************************************************************************************************
Program: Push Button Inputs & LEDs
Authour: Katleho Buthelezi
********************************************************************************************************/

// constants won't change.


// set pin numbers:
const int buttonPin1 = 6; // the number of the pushbutton pin
const int buttonPin2 = 5;
const int ResetPin = 3;

const int ledRED = 12;


const int ledBLUE = 10;
const int ledGREEN = 8 ; // the number of the LED pin
const int buzzer = 4;
int index = -1; // to indicate which led should be turned on
int index2 = 0;

// variables will change:


// variable for reading the pushbutton status

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);

digitalWrite(buzzer, 0); // Switch pressed, buzzer on


tone(buzzer, 100); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...

9
break;

case 2:
digitalWrite(ledBLUE, LOW);
digitalWrite(ledRED, HIGH);
digitalWrite(ledGREEN, LOW);

digitalWrite(buzzer, 0); // Switch pressed, buzzer on


tone(buzzer, 100); // Send 1KHz sound signal...
delay(1000); // wait for 1 sec
noTone(buzzer); // Stop sound...
break;

} //closing buttonState1

} //END of if (lastButtonState != buttonState)

//*******************************************************************************************************************************
buttonState2 = digitalRead(buttonPin2);

if (lastButtonState2 != buttonState2)
{
lastButtonState2 = buttonState2; //update to the new state

// if it is, the buttonState2 is HIGH/pressed:


if (buttonState2 == HIGH) // button is 2
{
index --; // turn on the next LED
if (index < 0) // if index is less than 2
{
index = 2;
}

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); // wait for 1 sec
noTone(buzzer); // Stop sound...
break;

case 1:
digitalWrite(ledBLUE, LOW);
digitalWrite(ledRED, LOW);
digitalWrite(ledGREEN, HIGH);

digitalWrite(buzzer, 0); // Switch pressed, buzzer on


tone(buzzer, 100); // Send 1KHz sound signal...
delay(1000); // Wait for 1 sec
noTone(buzzer); // Stop sound...
break;

case 2:
digitalWrite(ledBLUE, LOW);
digitalWrite(ledRED, HIGH);
digitalWrite(ledGREEN, LOW);

digitalWrite(buzzer, 0); // Switch pressed, buzzer on

10
tone(buzzer, 100); // Send 1KHz sound signal...
delay(1000); // wait for 1 sec
noTone(buzzer); // Stop sound...
break;
}
} //END of if (buttonState2 == HIGH)

} //END of if (lastButtonState2 != buttonState2)

//******************************************************************************************************************************
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)

} // closing void checkSwitches()

8. Discussion and conclusion

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

You might also like