Eee3933 - Lab Report 7
Eee3933 - Lab Report 7
COURSE NAME:
MICROPROCESSOR TECHNOLOGY LABORATORY
(EEE3933)
PROGRAMME:
BACHELOR OF ELECTRICAL AND ELECTRONICS ENGINEERING WITH HONOURS
LECTURER’S NAME:
YBHG. PROF. DR. TS. DR WAN ZUHA BIN WAN HASAN
LAB DEMONSTRATOR:
MUHAMMAD FARHAN BIN MUSTAFA
Procedure:
DC Motor Module:
Driver motor truth Table:
INPUT
IN1 IN2 EN MOTOR OUTPUT
0 0 1 STOP
0 1 1 TURN CW
1 0 1 TURN CCW
1 1 1 STOP
X X 0 STOP
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(IN1, OUTPUT); // motor direction
pinMode(IN2, OUTPUT); // motor direction
pinMode(EN, OUTPUT); // motor speed
// Turn off motor - Initial state
digitalWrite(IN1, LOW); // motor direction
digitalWrite(IN2, LOW); // motor direction
analogWrite(EN, 0); // motor speed
delay(2000);
}
4. The code is modified to make the motor speed accelerate and decelerate gradually and then motor
direction is changed when it reaches minimum speed before the motor starts to accelerate again.
Digital Input Module – Buttonsm Switches, Opto-Interrupter and Water Switch:
1. The Arduino pin is connected to the input module as follows:
ARDUINO A4 A5 13 12
if (buttonState == HIGH)
{digitalWrite(led1, HIGH);
}
else
{digitalWrite(led1, LOW);
}
if (switchState == HIGH)
{digitalWrite(led2, HIGH);
}
else
{digitalWrite(led2, LOW);
}
}
3. The code is uploaded, and BTN1 and SW1 are pushed and toggled. The outputs of LED1 and
LED2 are observed respectively. Observations are recorded, and the results are discussed.
4. The code is modified to include a variable that increments by 1 each time BTN1 is pressed. The
output is displayed on both the serial monitor and the LCD display.
Exercise:
A scenario is created using buttons, LEDs, and a DC motor where the output changes based on the
inputs provided. When a button is pressed, the motor rotates clockwise, and a corresponding LED
lights up. When another button is pressed, the motor rotates anticlockwise, and a different LED lights
up. If both buttons are pressed simultaneously, the motor stops, and both LEDs blink alternately.
Results:
DC Motor Module:
Listing 1.1: Modified code to make the motor speed accelerate and decelerate gradually and
then motor direction is changed when it reaches minimum speed before the motor starts to
accelerate again.
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(IN1, OUTPUT); // motor direction
pinMode(IN2, OUTPUT); // motor direction
pinMode(EN, OUTPUT); // motor speed
// Turn off motor - Initial state
digitalWrite(IN1, LOW); // motor direction
digitalWrite(IN2, LOW); // motor direction
analogWrite(EN, 0); // motor speed
delay(2000);
}
#include <LiquidCrystal.h>
// Pin definitions
const int buttonPin = A4; // Button pin
const int switchPin = A5; // Switch pin
const int led1 = 13; // LED1 pin
const int led2 = 12; // LED2 pin
void setup() {
// Pin modes
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(switchPin, INPUT);
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Counter: 0");
void loop() {
// Read button and switch states
buttonState = digitalRead(buttonPin);
switchState = digitalRead(switchPin);
// Update LCD
lcd.clear();
lcd.print("Counter: ");
lcd.print(counter);
}
void setup() {
// Set up the button pins as input
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
void loop() {
// Read the state of Button 1 (Clockwise)
button1State = digitalRead(button1Pin);
Discussion:
DC Motor Module:
Original code:
This Arduino code is designed to control a motor using a motor driver, showcasing basic operations
such as turning the motor on and off, controlling its direction, and varying its speed. The program
begins by defining three constants representing the motor driver’s pins: IN1, IN2, and EN. The IN1 and
IN2 pins control the motor's rotation direction, while the EN pin, which supports PWM (Pulse Width
Modulation), is used to adjust the motor's speed. These pins are configured as outputs to send signals
to the motor driver.
In the setup() function, the motor is initialized to an "off" state. The pinMode function sets the
direction and speed control pins as outputs. Both IN1 and IN2 are set to LOW, ensuring the motor
remains stationary. Simultaneously, the motor speed is set to 0 using analogWrite(EN, 0). A 2-
second delay (delay(2000)) is added to allow the system to stabilize before the loop begins,
ensuring that the motor starts from a known state.
The loop() function handles the motor's operation, running a sequence of commands repeatedly.
Initially, the motor is turned on with a specific direction by setting IN1 to HIGH and IN2 to LOW. The
speed is gradually increased in three steps: first at 0, then at a medium speed (analogWrite(EN,
180)), and finally at maximum speed (analogWrite(EN, 255)), with 2-second delays between each
step. This sequence allows the motor's behavior to be observed as its speed transitions from
stationary to full speed. After reaching maximum speed, the motor is turned off by setting both IN1
and IN2 to LOW, followed by another 2-second pause before the sequence restarts.
Listing 1.1:
This Arduino code demonstrates advanced motor control by gradually increasing and decreasing the
motor speed in a loop, alternating between two directions. The motor driver is controlled using three
pins: IN1, IN2, and EN. IN1 and IN2 determine the motor's rotation direction, while EN is a PWM-
enabled pin used to adjust the motor's speed. This setup allows for smooth acceleration and
deceleration, making it suitable for applications requiring precise motor control.
In the setup() function, the pins are configured as outputs using pinMode, enabling the Arduino to
send control signals to the motor driver. Initially, the motor is turned off by setting both IN1 and IN2 to
LOW and the speed to 0 using analogWrite(EN, 0). A 2-second delay ensures the system stabilizes
before starting the main loop.
The loop() function alternates between two motor directions, gradually increasing and then
decreasing the speed in each direction. For the first direction, IN1 is set to HIGH and IN2 to LOW. A
for loop increases the PWM value from 0 to 255 in increments of 5, with a 100-millisecond delay
between each step. This creates a smooth acceleration effect. After reaching maximum speed, the
motor holds this speed for 2 seconds before gradually decelerating back to 0 using another for loop.
The motor is then reversed by setting IN1 to LOW and IN2 to HIGH, and the process of acceleration,
holding, and deceleration is repeated.
Original Code:
This Arduino code demonstrates the control of two LEDs using a button and a switch. The program
reads the state of the button and switch and toggles the corresponding LEDs accordingly. This setup is
a basic example of digital input and output interaction with the Arduino, commonly used in simple
control systems or user interfaces.
The code begins by assigning constants to the pins connected to the button, switch, and LEDs. The
button is connected to pin A4, the switch to pin A5, LED1 to digital pin 13, and LED2 to digital pin 12.
These pins are configured in the setup() function. The LEDs are set as outputs using pinMode,
enabling the Arduino to control their states. Similarly, the button and switch pins are configured as
inputs to read their states.
The code begins by assigning constants to the pins connected to the button, switch, and LEDs. The
button is connected to pin A4, the switch to pin A5, LED1 to digital pin 13, and LED2 to digital pin 12.
These pins are configured in the setup() function. The LEDs are set as outputs using pinMode,
enabling the Arduino to control their states. Similarly, the button and switch pins are configured as
inputs to read their states.
The program uses if-else statements to control the LEDs based on the input states. This logic
ensures that each LED reflects the state of its respective control (button or switch). For instance,
LED1 lights up only when the button is pressed, and LED2 lights up only when the switch is toggled on.
This simple conditional control demonstrates how external inputs can directly influence output
devices.
Listing 2.1:
This Arduino program combines the use of a button, a switch, LEDs, and an LCD display to create a
simple interactive counter system. The code also integrates serial communication to output the
counter value to the Serial Monitor, making it versatile for debugging and demonstration purposes.
Here's a breakdown of its functionality:
The program starts by defining the pins for the button, switch, LEDs, and LCD. The button is
connected to pin A4, the switch to pin A5, LED1 to pin 13, and LED2 to pin 12. The LCD pins (rs, en,
d4, d5, d6, d7) are connected to digital pins 7, 6, 5, 4, 3, and 2, respectively. The LiquidCrystal
library is used to interface with the LCD. In the setup() function, the pins are configured as inputs or
outputs, the LCD is initialized to a 16x2 display, and the Serial Monitor is started at a baud rate of
9600.
The program uses a button press to increment a counter. To detect button presses accurately, the
code implements "rising edge detection." This means the counter is incremented only when the
button transitions from an unpressed (LOW) to a pressed (HIGH) state. The current button state is
stored in buttonState, and the previous state is tracked using lastButtonState. When a rising
edge is detected, the counter is incremented, and the updated value is displayed on both the LCD and
the Serial Monitor.
The LEDs are controlled based on the states of the button and the switch. LED1 (connected to pin 13)
lights up when the button is pressed, and LED2 (connected to pin 12) lights up when the switch is
toggled on. This behavior is implemented using a ternary operator for simplicity, which sets the LED
state to HIGH or LOW based on the input states.
The displayCounter() function is responsible for updating the counter value on both the LCD and
the Serial Monitor. The LCD display is cleared and updated with the new counter value, while the
Serial Monitor outputs the same value for debugging or logging purposes. This dual display approach
makes the system user-friendly and suitable for applications where real-time feedback is essential.
Exercise:
This Arduino code controls a motor and two LEDs using two buttons, one for clockwise rotation and
the other for anticlockwise rotation. The buttons, connected to pins A0 and A1, determine the
direction of the motor, and the LEDs, connected to pins 13 and 12, serve as indicators for the rotation
direction. The motor's direction is controlled by two pins (motorPin1 and motorPin2) connected to a
motor driver. The code uses button presses to trigger the motor's rotation, with the motor running for 1
second in each direction, followed by stopping.
The code begins by defining the pins for the buttons, LEDs, and motor control. The button pins are set
as inputs, while the LED and motor control pins are set as outputs in the setup() function. Initially,
both LEDs and the motor are turned off by setting the corresponding pins to LOW. This ensures that the
system starts in a neutral state, with no rotation or indication.
In the loop() function, the states of the two buttons are read using digitalRead. If Button 1
(Clockwise) is pressed, the motor rotates in the clockwise direction by setting motorPin1 to HIGH and
motorPin2 to LOW, while LED1 lights up to indicate clockwise rotation. After 1 second, the motor is
stopped by setting both motor control pins to LOW. If Button 2 (Anticlockwise) is pressed, the motor
rotates in the opposite direction, with LED2 lighting up to indicate anticlockwise rotation. The motor
runs for 1 second before stopping. This logic ensures that each button press triggers a specific
rotation direction and corresponding LED indicator.
To prevent button bouncing (when a button press is mistakenly detected multiple times), a small delay
of 100 milliseconds is added after each button press. Additionally, the states of the buttons are stored
in lastButton1State and lastButton2State to detect changes in button states (rising edges). This
ensures that the motor only starts rotating when a new button press is detected, preventing multiple
triggers from a single press.
Conclusion:
From this laboratory, I obtained a hands-on experience with key concepts in embedded systems, such
as digital input/output control, PWM for motor speed adjustment, and interfacing with peripherals like
LEDs and LCDs. Through these exercises, I gain practical skills in integrating hardware and software,
problem-solving, and debugging, which are essential for real-world applications. By breaking down
complex tasks into manageable steps, such as detecting button presses or gradually adjusting motor
speed, I have developed critical thinking and a deeper understanding of microcontroller programming
References:
• Banzi, M., & Shiloh, M. (2014). Arduino workshop: A hands-on introduction with 65 projects
(2nd ed.). McGraw-Hill Education.
• Monk, S. (2016). Programming Arduino: Getting started with sketches (2nd ed.). McGraw-Hill
Education.
• Arduino. (n.d.). Arduino - Home. Arduino. Retrieved December 9, 2024, from
https://www.arduino.cc/
Appendix:
Simulation videos
Listing 1.1:
https://drive.google.com/file/d/1WjUs6ziJRmd88vyrUpPDhAPw9RH_kLjQ/view?usp=sharing
Listinng 2.1:
https://drive.google.com/file/d/1A1cLql9PSgUOFWod3HSM7ULWTbcOqYBM/view?usp=sharing