0% found this document useful (0 votes)
5 views15 pages

Eee3933 - Lab Report 7

This lab report details the procedures and results of experiments using a Microcontroller DH PIC Training Kit, focusing on DC Motor and Digital Input modules. The report includes code examples for controlling motor speed and direction, as well as handling button inputs to control LEDs and a motor. Observations and discussions on the results highlight the effectiveness of the implemented codes and their modifications.

Uploaded by

Hakimi Rifhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Eee3933 - Lab Report 7

This lab report details the procedures and results of experiments using a Microcontroller DH PIC Training Kit, focusing on DC Motor and Digital Input modules. The report includes code examples for controlling motor speed and direction, as well as handling button inputs to control LEDs and a motor. Observations and discussions on the results highlight the effectiveness of the implemented codes and their modifications.

Uploaded by

Hakimi Rifhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

LAB REPORT 7:

Microcontroller DH PIC Training KIT – Output and Input Module


SEMESTER 1, 2024/2025

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

NAME: MUHAMMAD RIFHAN HAKIMIE BIN ROSLI


MATRICS NO: 225361

DATE: 18 DECEMBER 2024


SUBMISSION DATE: 31st DECEMBER 2024
TABLE OF CONTENTS
Tools: 3
Procedure: 3
DC Motor Module: 3
Digital Input Module – Buttonsm Switches, Opto-Interrupter and Water Switch: 5
Results: 6
DC Motor Module: 6
Digital Input Module: 8
Discussion: 11
DC Motor Module: 11
Original code: 11
Listing 1.1: 12
Digital Input Module: 12
Original Code: 12
Listing 2.1: 13
Conclusion: 14
References: 14
Appendix: 15
Tools:
• Arduino Training Kit
• Personal Laptop

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

1. Arduino pin is connedted to the DC motor module as follows,


ARDUINO 4 3 2

MODULE IN1 IN2 EN


2. The Arduino IDE is opened and the following codes are typed into a new sketch
const int IN1 = 4, IN2 = 3, EN = 2;

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

// the loop function runs over and over again forever


void loop() {
// Turn on motor
digitalWrite(IN1, HIGH); // motor direction
digitalWrite(IN2, LOW); // motor direction
analogWrite(EN, 0); // motor speed
delay(2000);
analogWrite(EN, 180); // motor speed
delay(2000);
analogWrite(EN, 255); // motor speed
delay(2000);
// Turn off motor
digitalWrite(IN1, LOW); // motor direction
digitalWrite(IN2, LOW); // motor direction
delay(2000);
}

3. The code is uploaded and the observation is recorded and observed.

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

MODULE BTN1 S1 LED1 LED2


2. The Arduino IDE is opened and the following codes are typed into a new sketch:

const int buttonPin = A4;


const int switchPin = A5;
const int led1 = 13; // the number of the LED1 pin
const int led2 = 12; // the number of the LED2 pin
// variables will change:
int buttonState = 0; int switchState = 0;
void setup()
{
pinMode(led1, OUTPUT); pinMode(led2, OUTPUT);
pinMode(buttonPin, INPUT); pinMode(switchPin, INPUT);
}
void loop()
{buttonState = digitalRead(buttonPin); switchState = digitalRead(switchPin);

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.

const int IN1 = 4, IN2 = 3, EN = 2;

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

// the loop function runs over and over again forever


void loop() {
// Set motor direction
digitalWrite(IN1, HIGH); // motor direction
digitalWrite(IN2, LOW); // motor direction

// Gradually increase motor speed


for (int speed = 0; speed <= 255; speed += 5) {
analogWrite(EN, speed);
delay(100); // Adjust delay to control the rate of speed increase
}

delay(2000); // Hold at max speed for 2 seconds

// Gradually decrease motor speed


for (int speed = 255; speed >= 0; speed -= 5) {
analogWrite(EN, speed);
delay(100); // Adjust delay to control the rate of speed decrease
}

// Turn off motor


digitalWrite(IN1, LOW); // motor direction
digitalWrite(IN2, HIGH); // motor direction
delay(200);

// Gradually increase motor speed


for (int speed = 0; speed <= 255; speed += 5) {
analogWrite(EN, speed);
delay(100); // Adjust delay to control the rate of speed increase
}

delay(2000); // Hold at max speed for 2 seconds

// Gradually decrease motor speed


for (int speed = 255; speed >= 0; speed -= 5) {
analogWrite(EN, speed);
delay(100); // Adjust delay to control the rate of speed decrease
}

Figure 1.1: Circuitry of the code on PIC training kit V2


Digital Input Module:
Listing 2.1: Modified code 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.

#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

// LCD pin definitions


const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Variables to track states


int buttonState = 0; // Current button state
int lastButtonState = 0; // Previous button state
int switchState = 0; // Switch state
int counter = 0; // Counter variable

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

// Initialize Serial Monitor


Serial.begin(9600);
Serial.println("Counter: 0");
}

void loop() {
// Read button and switch states
buttonState = digitalRead(buttonPin);
switchState = digitalRead(switchPin);

// Check if button is pressed (rising edge detection)


if (buttonState == HIGH && lastButtonState == LOW) {
counter++; // Increment the counter
displayCounter(); // Update LCD and Serial Monitor
}
lastButtonState = buttonState; // Save the current button state

// Control LEDs based on button and switch states


digitalWrite(led1, buttonState == HIGH ? HIGH : LOW);
digitalWrite(led2, switchState == HIGH ? HIGH : LOW);
}

// Function to display the counter value


void displayCounter() {
// Update Serial Monitor
Serial.print("Counter: ");
Serial.println(counter);

// Update LCD
lcd.clear();
lcd.print("Counter: ");
lcd.print(counter);
}

Figure 2.1: Circuitry of the code on PIC training kit V2


Exercise:
// Pin definitions
const int button1Pin = A0; // Button 1 pin (Clockwise)
const int button2Pin = A1; // Button 2 pin (Anticlockwise)
const int led1Pin = 13; // LED1 pin (Clockwise indication)
const int led2Pin = 12; // LED2 pin (Anticlockwise indication)

// Motor control pins


const int motorPin1 = 9; // Motor control pin 1 (IN1 on motor driver)
const int motorPin2 = 10; // Motor control pin 2 (IN2 on motor driver)

int button1State = 0; // Button 1 state


int button2State = 0; // Button 2 state
int lastButton1State = 0; // Last state of button 1
int lastButton2State = 0; // Last state of button 2

void setup() {
// Set up the button pins as input
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);

// Set up the LED pins as output


pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);

// Set up motor control pins as output


pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);

// Initialize LEDs to be off


digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);

// Initialize motor pins to be off


digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}

void loop() {
// Read the state of Button 1 (Clockwise)
button1State = digitalRead(button1Pin);

// Read the state of Button 2 (Anticlockwise)


button2State = digitalRead(button2Pin);

// Check if Button 1 was pressed (Clockwise)


if (button1State == HIGH && lastButton1State == LOW) {
// Rotate motor clockwise
digitalWrite(led1Pin, HIGH); // Turn on LED1 (Clockwise indicator)
digitalWrite(led2Pin, LOW); // Turn off LED2
digitalWrite(motorPin1, HIGH); // Motor rotates clockwise
digitalWrite(motorPin2, LOW); // Motor rotates clockwise
delay(1000); // Run the motor for 1 second (you can adjust this time)
digitalWrite(motorPin1, LOW); // Stop motor after rotation
digitalWrite(motorPin2, LOW); // Stop motor after rotation
}

// Check if Button 2 was pressed (Anticlockwise)


else if (button2State == HIGH && lastButton2State == LOW) {
// Rotate motor anticlockwise
digitalWrite(led1Pin, LOW); // Turn off LED1
digitalWrite(led2Pin, HIGH); // Turn on LED2 (Anticlockwise indicator)
digitalWrite(motorPin1, LOW); // Motor rotates anticlockwise
digitalWrite(motorPin2, HIGH); // Motor rotates anticlockwise
delay(1000); // Run the motor for 1 second (you can adjust this time)
digitalWrite(motorPin1, LOW); // Stop motor after rotation
digitalWrite(motorPin2, LOW); // Stop motor after rotation
}

// Update the last button states


lastButton1State = button1State;
lastButton2State = button2State;

// A small delay to prevent button bouncing


delay(100);
}

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.

Digital Input Module:

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

Original code for DC Motor: https://drive.google.com/file/d/10RhvjYncfVw-3pctg6U1zgnP--


KjZf60/view?usp=sharing

Listing 1.1:
https://drive.google.com/file/d/1WjUs6ziJRmd88vyrUpPDhAPw9RH_kLjQ/view?usp=sharing

Original code for Digital Input Module:


https://drive.google.com/file/d/1UIZmgWXnGB3SoIzOWS1pCH0LyKmf1fsf/view?usp=sharing

Listinng 2.1:
https://drive.google.com/file/d/1A1cLql9PSgUOFWod3HSM7ULWTbcOqYBM/view?usp=sharing

You might also like