0% found this document useful (0 votes)
34 views57 pages

Arduino Mini Course RB

Uploaded by

Raheem Bux
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)
34 views57 pages

Arduino Mini Course RB

Uploaded by

Raheem Bux
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/ 57

Arduino

Mini-Course
Lecture Slides

1
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Signals

2
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
What is a Digital Signal?

● Like a light switch, can be “On” or “Off”


● On
○ Supply voltage, VCC, or 5V
○ “High”, “1”
● Off
○ Ground, GND, or 0V
○ “Low”, “0”
● Digital signals can be a series of 1’s and 0’s

3
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
How to Use a Digital Signal
● 14 digital input-output (IO) on Arduino UNO
○ Pins 0 – 13
● Digital output
○ Arduino to another device
○ Use digitalWrite() function
● Digital input
○ Another device to Arduino
○ Use digitalRead() function
● Pin 13 is connected to an on-board LED

4
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Output Example

5
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Output Example: Outline

Blinky Example:
● The “Hello World” for microcontrollers
● Use the on-board LED
○ Turn ON for half a second
○ Turn OFF for half a second

6
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Output Example: Problem Breakdown

How do we get the LED to turn on and off?


● LED is connected to pin 13
● Signal comes from the Arduino to the LED – Digital Output
● Send signal to turn ON (“High”) and OFF (“Low”)

7
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Turning the LED On and Off: pinMode()

Description: Syntax:
Configures the specified pin to pinMode(pin, mode)
behave either as an input or an Parameters:
output. pin: the Arduino pin number to set the
Example: mode of.
void setup() {
mode: INPUT, OUTPUT, or INPUT_PULLUP.
// Sets the digital pin 13 Returns:
// as output Nothing
pinMode(13, OUTPUT);
}

8
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Output Example: Problem Breakdown

How do we get the LED to turn on and off?


● LED is connected to pin 13
● Signal comes from the Arduino to the LED – Digital Output
● Send signal to turn ON (“High”) and OFF (“Low”)

9
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Turning the LED On and Off: digitalWrite()

Description: Syntax:
Write a HIGH or a LOW value to a digitalWrite(pin, value)
digital pin. Parameters:
pin: the Arduino pin number
Example: value: HIGH or LOW
void loop() { Returns:
// Sets digital pin 13 on Nothing
digitalWrite(13, HIGH);
}

10
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Output Example: Problem Breakdown

How do we get the LED to turn on and off?


● LED is connected to pin 13
● Signal comes from the Arduino to the LED – Digital Output
● Send signal to turn ON (“High”) and OFF (“Low”)

11
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Output Example: Problem Breakdown

How do we get the LED to turn on and off?


● LED is connected to pin 13
● Signal comes from the Arduino to the LED – Digital Output
● Send signal to turn ON (“High”) and OFF (“Low”)
How do we get the Arduino to wait?
● Use a specific built-in function

12
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Get the Arduino to Wait: delay()

Description: Syntax:
Pauses the program for the amount delay(ms)
of time (in milliseconds) specified as Parameters:
parameter. ms: the number of milliseconds to pause.
Example: Returns:
void loop() { Nothing
digitalWrite(13, HIGH);
delay(1000); // Wait for 1 second
digitalWrite(13, LOW);
delay(1000); // Wait for 1 second
}

13
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Output Example: Problem Breakdown

How do we get the LED to turn on and off?


● LED is connected to pin 13
● Signal comes from the Arduino to the LED – Digital Output
● Send signal to turn ON (“High”) and OFF (“Low”)
How do we get the Arduino to wait?
● Use a specific built-in function

14
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Breadboards

15
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
How to Use a Breadboard

● Helps prototype circuits quickly


● Full of wires to make creating circuits
faster and cleaner
● Red positive (+) rails are the supply
voltage (5 V)
● Black negative (-) rails are ground (GND or
0V)
● Middle space (A – J) are is where the
components go

16
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Breadboard Practice Problems
# Question Answer

1 Is hole A1 connected to B1?

2 Is hole D20 connected to J22?

3 Is hole A5 connected to A6?

4 Is hole G15 connected to J15?

5 Is hole A10 connected to I10?

6 Is hole E30 connected to F30?

7 Is hole (- bottom rail)1 connected to (- bottom rail)25?

8 Is hole (- bottom rail)1 connected to (+ bottom rail)1?

9 Is hole (- top rail)1 connected to (- bottom rail)25?

10 Is hole (+ top rail)1 connected to J1?

17
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Breadboard Practice Problems: Answers
# Question Answer

1 Is hole A1 connected to B1? Yes

2 Is hole D20 connected to J22? No

3 Is hole A5 connected to A6? No

4 Is hole G15 connected to J15? Yes

5 Is hole A10 connected to I10? No

6 Is hole E30 connected to F30? No

7 Is hole (- bottom rail)1 connected to (- bottom rail)25? Yes

8 Is hole (- bottom rail)1 connected to (+ bottom rail)1? No

9 Is hole (- top rail)1 connected to (- bottom rail)25? No

10 Is hole (+ top rail)1 connected to J1? No

18
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Input Example

19
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Input Example: Outline

LED and Switch Example:


● When switch is on, LED turns on
● When switch is off, LED turns off

20
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() function
How do we read the value from the switch?
● Setup the switch hardware
● Signal comes from the switch to the Arduino - Digital Input
● When switch is on, turn LED on
● When switch is off, turn LED off

21
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() function
How do we read the value from the switch?
● Setup the switch hardware
● Signal comes from the switch to the Arduino - Digital Input
● When switch is on, turn LED on
● When switch is off, turn LED off

22
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Reading a Digital Pin: digitalRead()

Description: Syntax:
Reads a HIGH or LOW value from a digitalRead(pin)
digital pin. Parameters:
pin: the Arduino pin number to read.
Example: Returns:
void loop() { HIGH or LOW
// Reads pin 13 and stores
// result into val
int val = digitalRead(13);
}

23
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() function
How do we read the value from the switch?
● Setup the switch hardware
● Signal comes from the switch to the Arduino - Digital Input
● When switch is on, turn LED on
● When switch is off, turn LED off

24
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
If-Else Statement

Description: Example:
A statement used when you have if(x < 5)
multiple different actions to choose {
from. // Run if x is less than 5
}
Syntax:
else
if(conditional statement){
{
// Run if statement is true
// Run if x is greater than
}
// or equal to 5
else{
}
// Run if statement is false
}

25
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() function
How do we read the value from the switch?
● Setup the switch hardware
● Signal comes from the switch to the Arduino - Digital Input
● When switch is on, turn LED on
● When switch is off, turn LED off

26
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Digital Signal Challenge Project

Use the LED to output a message in morse code and a


switch to control the message.
Requirements:
1. Must use variables or constants only within void
loop()
○ Ex: Use delay(variableName) instead of
delay(500)
2. Must output a word that is 3 or more letters long
3. The switch must turn on and off the morse code
message

27
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Signals

28
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
What is an Analog Signal?

● Like a dimmable light dial


○ Can be fully off or on
○ But can also be turned to halfway brightness or any value in between
● An analog signal can be any voltage between ground and the supply voltage (5V)

29
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
How to Use an Analog Signal

● 6 analog inputs on Arduino UNO


○ Pins A0 – A5
● Analog input
○ Uses an “ADC” to convert it to a digital
signal
○ Use analogRead() function
● No analog output

30
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example

31
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Outline

LED and Potentiometer Example:


● Start with blinking LED
● As the potentiometer gets turned up, LED
blinks faster
● As the potentiometer gets turned down,
LED blinks slower

32
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() and delay() function
How do we read the value from the potentiometer?
● Setup the potentiometer hardware
● Analog signal comes from the potentiometer to the Arduino - Analog Input
● Map ADC value to the delay in the LED blinking code

33
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() and delay() function
How do we read the value from the potentiometer?
● Setup the potentiometer hardware
● Analog signal comes from the potentiometer to the Arduino - Analog Input
● Map ADC value to the delay in the LED blinking code

34
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Reading a Digital Pin: analogRead()

Description: Syntax:
Reads the integer value from an analogRead(pin)
analog pin. Parameters:
pin: the Arduino analog input pin to read.
Example: Returns:
void loop() { The analog reading on the pin as an int
// Reads pin A0 and stores (value between 0-1023).
// result into val
int val = analogRead(A0);
}

35
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() and delay() function
How do we read the value from the potentiometer?
● Setup the potentiometer hardware
● Analog signal comes from the potentiometer to the Arduino - Analog Input
● Map ADC value to the delay in the LED blinking code

36
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() and delay() function
How do we read the value from the potentiometer?
● Setup the potentiometer hardware
● Analog signal comes from the potentiometer to the Arduino - Analog Input
● Map ADC value to the delay in the LED blinking code

37
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() and delay() function
How do we read the value from the potentiometer?
● Setup the potentiometer hardware
● Analog signal comes from the potentiometer to the Arduino - Analog Input
● Map ADC value to the delay in the LED blinking code
How do we make it run smoother?
● When potentiometer is turned all the way down, turn LED completely off
● When potentiometer is turned all the way up, turn LED completely on

38
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we get the LED to turn on and off?


● Use digitalWrite() and delay() function
How do we read the value from the potentiometer?
● Setup the potentiometer hardware
● Analog signal comes from the potentiometer to the Arduino - Analog Input
● Map ADC value to the delay in the LED blinking code
How do we make it run smoother?
● When potentiometer is turned all the way down, turn LED completely off
● When potentiometer is turned all the way up, turn LED completely on

39
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Challenge Project

Make an LED pattern based on the position


of the potentiometer using several LEDs.
Requirements:
1. Must use at least 3 LEDs
2. Must have at least 3 different patterns.

40
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
PWM Signals

41
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
What is a PWM Signal?

● Pulse Width Modulation


● Consists of a repeating pattern of digital
pulses
● The width of the pusles can change to
achieve different results

42
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
What is a PWM Signal?

Frequency: Duty Cycle:


● Measured in Hz or 1/s ● Measured in %

43
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
What is a PWM signal?

● Used to simulate an analog signal


● Average voltage over time can be any value between 0V and 5V

44
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
How to Use a PWM Signal

● Uses pins 3, 5, 6, 9, 10, and 11


● Use analogWrite() function
● Pins 5, 6 are 980 Hz
● Pins 3, 9, 10, 11 are 490 Hz

45
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
PWM Example

46
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
PWM Example: Outline

Dimming LED Example:


● LED gets brighter and brighter
● Then, gets dimmer and dimmer

47
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we control the brightness/dimness of an LED?


● Use PWM to control the average voltage/current to the LED
● Higher duty cycle → higher average voltage/current → brighter LED
How do we control the PWM duty cycle to get the LED pattern we want?
● Brighter and brighter → Slowly increase duty cycle
● Dimmer and dimmer → Slowly decrease duty cycle

48
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Write a PWM Signal: analogWrite()

Description: Syntax:
Writes an analog value (PWM wave) analogWrite(pin, value)
to a digital pin. Parameters:
pin: the Arduino pin to write, must be a
Example: PWM pin market with a ‘~’.
void loop() {
value: the duty cycle (value between
// Writes a 50% duty cycle to
0-255)
// pin 11 Returns:
analogWrite(11, 255*0.5); None
}

49
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we control the brightness/dimness of an LED?


● Use PWM to control the average voltage/current to the LED
● Higher duty cycle → higher average voltage/current → brighter LED
How do we control the PWM duty cycle to get the LED pattern we want?
● Brighter and brighter → Slowly increase duty cycle
● Dimmer and dimmer → Slowly decrease duty cycle

50
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
For Loop

Description: Example:
A repeating loop of code for as long for(int i = 0; i < 7, i++){
as a certain condition is true. // Run while i remains less
// than 7, every loop i
// increases by 1
}

Syntax:
for(initializer; conditional statement; iterator)
{
// Run while statement is true
}

51
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we control the brightness/dimness of an LED?


● Use PWM to control the average voltage/current to the LED
● Higher duty cycle → higher average voltage/current → brighter LED
How do we control the PWM duty cycle to get the LED pattern we want?
● Brighter and brighter → Slowly increase duty cycle
● Dimmer and dimmer → Slowly decrease duty cycle
● Use for loop for repeating pattern

52
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Analog Input Example: Problem Breakdown

How do we control the brightness/dimness of an LED?


● Use PWM to control the average voltage/current to the LED
● Higher duty cycle → higher average voltage/current → brighter LED
How do we control the PWM duty cycle to get the LED pattern we want?
● Brighter and brighter → Slowly increase duty cycle
● Dimmer and dimmer → Slowly decrease duty cycle
● Use for loop for repeating pattern

53
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
PWM Challenge Project

Add PWM functionality to the Analog Input


Challenge Project.
Requirements:
1. Must use at least 3 LEDs
2. Must have at least 3 different patterns.
3. Must have at least 2 different patterns
that include dimming/brightening of
LEDs.

54
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Course Recap

55
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Key Concepts Check

Can you define each term? Do you know what each function does?
● Digital signals ● pinMode()
● Analog signals ● digitalWrite()
● PWM signals ● delay()
● digitalRead()
● analogRead()
● analogWrite()

56
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved
Final Project

Make a LEDs behaviour changes based on


the position of a potentiometer and switch.
Requirements:
1. Use all signal types covered in this
course
○ Digital input and output
○ Analog input
○ PWM signal

57
© Copyright 2022 Gentiam Consulting LLC - All Rights Reserved

You might also like