0% found this document useful (0 votes)
7 views6 pages

Arduino LED Control with PWM and Switch

Arduino summery

Uploaded by

harrisrohaan907
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)
7 views6 pages

Arduino LED Control with PWM and Switch

Arduino summery

Uploaded by

harrisrohaan907
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

Arduino Activity #2

In this activity we are going to learn how to make an input pin so we can control a light with a
push button. We will also learn what Pulse Width Modulation is and how we can use it with
LEDs. PLEASE MAKE A NEW FILE FROM ACTIVITY 1 AND NOT JUST MODIFY ACTIVITY
1!

Step 1:
Place your arduino in TinkerCAD. Note there is a button labeled “Code”. This is where we can
program our arduino. Then select the drop down that says “Blocks” and change it to “text”. We
are programmers. We don’t need blocks anymore. We will be coding in C++. It is similar to
Python with some differences in syntax. We will learn some basic C++ over the next few
activities. This is what your screen should look like:

v
Step 2:
Add an LED with appropriate resistor to pin “3”. Your circuit should look like this: (note the
resistor value is not correct, you should be able to calculate this from the last activity).

Step 3:
Change the code that blinks the “LED_BUILTIN” to blink the LED connected to pin “3”. This is
the same as activity 1.
Step 4:
Now we are going to add an input to control the LED. First wire a switch so the output of pin “4”
goes to one side of the switch and the other side goes to ground. Your circuit should look
something like this: (Note the resistor is not the correct value, you calculated this value in the
last activity).
Step 5:
Now we need what is called a “bleed resistor” between pin 4 and ground. The idea of this
resistor is to bleed any charge between the switch and pin 4 when the switch is turned off to
make sure pin 4 will eventually read LOW. If this is not included then there may be a charge
stored between pin 4 and the switch which means pin 4 will never read LOW. A 1000 Ohm
resistor will be sufficient. Your circuit should look like this:

Step 6:
Now we need to let the program know that we are going to use pin “4” as an INPUT. If the code:
pinMode(3, OUTPUT);
Configures pin “3’” as an OUTPUT, how do you think we can change this to configure pin “4” as
an INPUT? Please add the appropriate lines of code to your program. DON’T FORGET THE
SEMICOLON!

Step 7:
Now we need to add a conditional statement. Like in python we will use “IF”. The exact syntax is
C++ is:
If (condition) {
//Block of code that is executed if the condition is true
}
In C++ the curly braces indicate what is included in the IF statement where in Python the
indenting determined what was included in the IF statement. Indenting in C++ is optional but
strongly recommended to make your code easier to read.

For inputs to the digital pins we use the command:


digitalRead(#);
To read if the signal at pin # is HIGH or LOW. For example if we want to check if the signal at pin
“4” is high or low we use the code:
digitalRead(4);

So we want the switch to run on the LED. When the switch is on pin 4 is connected to 5V which
is considered “HIGH”. So if pin “4” equals HIGH then the block of code that blinks the light
should be executed. If it is not HIGH then nothing should happen. Please modify the code
accordingly. If stuck please ask Mr. Uniat for assistance. This is how we can make inputs control
various things using the Arduino.

Step 8: (answer questions at the bottom of the document)


In the last activity we changed the delay time from 1000ms to 500ms so the LED would be on
for less time. What happens if we keep decreasing these values? What happens if you change
BOTH delays from 1000 to 100 this time? Now what happens if you change BOTH delays to
10? How does the brightness compare to when the LED was ON before?

Step 9: (answer questions at the bottom of the document)


Increase the time of the delay command after the HIGH command to 15ms and decrease the
delay command after the LOW command to 5ms. What happened to the brightness? Why did
this happen if the total combined delay is still 20ms?

Now decrease the time of the delay command after the HIGH command to 5ms and increase
the delay command after the LOW command to 15ms? What happened to the brightness? Why
did this happen if the total combined delay is still 20ms?
This is called Pulse Width Modulation (PWM). By switching digital inputs very quickly we can
control the brightness of LEDs or the speed of electric motors even though they are technically
either ON or OFF when connected to a digital output. The average of the amount of time the
LED is ON is called the “duty cycle”. This switching needs to be very quick to fool the human
eye. The LED in this activity is switched on and off once every 20ms or 50 times a second. We
call this 50Hz. Since the LED is switched on and off every 20ms we say the “period” is 20ms.
Note how the period did not change for PWM.
Step 10:
Now let’s build the circuit! Start by building the circuit like you did in lab 1. Now you are going to
add a switch and a bleed resistor. For the bleed resistor use a 10,000 Ohm resistor (because
[Link] has way more 10,000 Ohm resistors than 1000 Ohm resistors). For the switch use a
push button. Use a multimeter to figure out which way to wire the button. Write the code so
when the button is pressed the light turns on.
Extension: wire up all 3 RGB colors to 3 different buttons and have them each turn on when a
button is pressed. What happens when you press multiple buttons at the same time?

Questions:
What happens to the brightness when you increase or decrease BOTH delays (question from
step 8)?

What happens to the brightness when you increase the HIGH delay to 15ms and decrease the
LOW delay to 5ms (question from step 9)?

What happens to the brightness when you increase the LOW delay to 15ms and decrease the
HIGH delay to 5ms (question from step 9)?

Picture:
Please paste a picture below of your real world circuit!

You might also like