That’s right folks, it’s Monday, and the second installment of my arduino tutorial series is here! I spent the last week in Disney World, but now that I’m back, it’s time to get down to business! This week, I’ll show you how to read a push button, debounce its input using software, how to create a function in the arduino programming environment, and how to change LED brightness using Pulse Width Modulation (PWM). If you have no idea what PWM is, or if you are confused about analog and digital signals, go check out my TechBits episode on the topic first! Don’t forget to go browse the element14 Arduino Group; it’s a great place to post questions, and learn from others!
You can download the files associated with this episode here:
A simple schematic of the connections would be nice. To you it is easy. I sort of found the connections to the button confusing they way you presented them.
Thanks for the feedback! I’m going to upload schematics, and include schematic screenshots with the new videos so that this is more clear. You’ll see it in the new video later today.
I have been in the electronics field for over 40 years but for the last 6 I have been a supervisor and therefore not up to speed on the latest things. Your sight is great for those that want to learn about what is going on today not 5 years ago. Thanks for your willingness to give of your time and expertise. You also do a great job as an instructor because you cover all points and explain as you go. This is not only good for the newcomer but for those of us that have not been directly involved with design but just repair.
Love the tutorials, I just finished #1 and am looking to do #2 next.
Did you know that the product you link to in the parts list for “Pushbutton” is only available in a minimum order quantity of 10,000! Do you have an alternate?
Thanks for the great site! I am learning arduino so I can bring some products to the market and your tutorials have been my guiding light.
Quick issue (and if you don’t have time for this I completely understand)
In tutorial #2 I have been over the video many times and keep getting a compile error. ‘ledLevel was not declared in this scope.’
probably a syntax error, but I have no idea what is going wrong. So, I was wondering if 1) you would look at my code and point me to the error, 2) if you would post the text of the codes in your videos so I can work through syntax without bothering you!
Thanks again,
Wake Gregg
int switchPin = 8;
int ledPin = 11;
boolean lastButton =LOW;
boolean currentButton = LOW;
int ledLevel = 0;
In your last block you write:
}
lastButton = currentButton;
if (ledLevel > 255) LedLevel = 0;
analogWrite (ledPin, ledLevel);
}
If you can see you wite LedLevel (first L in uppercase) and ledLevel (first l in lowercase), I am not an expert but i have heard that the program is case sensitive.
Just to expand on the latching/debounced button thing, how hard would it be to have multiple buttons/LED’s doing this independently? I’m new to this coding stuff and can’t seem to figure out where to start with what I want to do. I can set up multiple buttons controlling multiple LED’s momentarily, and I can do the latching thing like you did in this video with one button/switch, but I can’t work out how to combine it all.
thank you for your great tutorials!
But there is one thing i dont understand.
You are using a 1k or 10k ohm resistor, to connect the button to the ground. But why the current runs to the ground through the resistor and not to the digital-arduino-pin?
When the button is unpressed, the arduino pin is connected to gnd through the resistor, so it reads a logic 0. When the button is pressed, current can flow from two possible places, the 5V rail, or gnd through the resistor. The arduino detects a logic 1 when the button is pressed because the current from the 5V rail far outweighs the current that goes to ground, do to the pull down resistor.
Hey, very interesting explanation. I did not get though the last sentence: “…5V rail far outweighs the current that goes to ground, do to the pull down resistor.” Did you mean “due to the pull down resistor”? I am not quite sure that I understand this sentence.
Best regards,
Galina
Thanks a ton for this arduino video tutorial series!! These videos have really been helpful for a beginner like me to understand what’s going on. In this video you are using resistors, but I don’t really understand why. Can you link me to a video that provides an explanation of what resistors are used for and when to use them? That would be really helpful. Thanks!
I have a problem :( i used yours code – after mine failed (allready @ example 1).. i connected completely the same (used 10k resistor) … hm, the problem is, the led diode is on after i upload code on arduino… then i push the button and it doesn’t happen anything or the led becomes lit a bit stronger! Sometimes LED also blinks without doing anything (i have a feeling like it’s a short circuit or something isn’t connected well) – but i checked it several times, so i’m rly not sure what did i do wrong
Please check whether the the legs of the switch which you are using are not already short. Actually in the 4 legged switch which JEREMY used in his tutorial – 2 legs are already shorted.
Solution to your problem – Make the connections on the diagonally opposite legs of the switch ..!!
Great site. Going Biomedical and we are learning Processing and the Arduino. WIll be going throught all youor tutorial. Great teaching method and preparation for all these Tutorial. Old timer as well back to school
Thanks for the code Jeremy, it worked great though I had to make a few changes for it to work in my circuit. By the way, your website videos don’t work that well, there seems to be a buffering problem. The ones on You Tube worked just fine.
Hi Jeremy, thanks for the great video!
I have a question for you or the other readers:
I want to ad 2 buttons to the circuit. In total 3 buttons.
They are the input in Pin 14, 15 and 16
Connected to pins 0, 1 and 2 is a light circuit.
I want pin 0 to give 5V if I press the button connected to pin 14,
pin 1 give 5V if I press button connected to 15
pin 2 give 5V if I press button connected to 16
I used te code from your movie and coppied it to add more inputs and outputs.
But it doesn’t work… What am I doing wrong?
This is the code:
// INPUTS
int switch01_Pin = 14;
int switch02_Pin = 15;
int switch03_Pin = 16;
// OUTPUTS
int wire01_Pin = 0;
int wire02_Pin = 1;
int wire03_Pin = 2;
you didn’t modify the debounce function correctly. By putting “;” after the if statements, you’re making them do nothing. You need to put the content for each if statement within {} like I did in the video.
I am debouncing one switch, then toggling all three wires. Is that correct? I want wire01 on or off if I press switch01, wire02 by switch02 and wire03 by switch03. How do I debounce the three switches?
You need to debounce each one. You could modify the debounce function so that it takes the switch as an argument too, like this:
boolean debounce(boolean last, int switch)
{
boolean current = digitalRead(switch);
if (last != current)
{
delay(5);
current = digitalRead(switch);
}
return current;
}
That way, you can call it for each switch, by changing the second argument.
I am a serious beginner, so you could expect a very beginner questions from me…what is it when you change from digital input to analog input. what are the differences between those two?
I’m trying something similar to your PWM control of an led, but with a solid state relay hooked up to a standard incandescent household light bulb (57W) that uses AC. Instead of a button, I tried to just show each dimming level for 5 seconds. It doesn’t work out too well, but instead seems to flicker on and off a few times per seconds (as far as my eye can tell). Is it just a bad idea to use PWM to control an AC device? My ultimate goal is to have a toaster oven that is PID temperature controlled so maybe PWM would have better success with a heating element since temperature changes relatively slowly? This is my test code:
You shouldn’t try to control an AC line with PWM. It’s not the same as a DC signal, and it won’t work as you expect. Controlling the heating element directly makes more sense.
Currently working on to power the led and manually controlling the led with variable resistor. That part was easy. Im trying to enhance the circuit by adding two pushbuttons which function is to make the led either go dimmer or brighter. any suggestion on the codes and connection?
Is there a way to attach multiple LEDs to a single PWM, say if I want to control more LEDs than ports I have? I’m assuming some sort of external chip that could toggle between them.
I’m completely new to any sort of coding that isn’t on a TI-84 or modding Pokemon games. With your videos I’ve learned all the basics of Arduino in a couple hours. THANKS A TON!! a quick question though, the different I.D’s you make for the commands (e.g ledPin, or buttonPin etc.) do those actually matter? can i just make it like “lolPin” and “thatreallylongnamedPin”? if the DO matter, how would i use more than one of the same thing? Thanks again.
Hey Jeremy
Great work.. Thank you
I am doing a similar project to this one but using 2 buttons to control led brightness- 1 for up 1 for down.. I am wondering how to debounce more than 1 button in the code – I cannot seem to get it to work…
Using your code Switch 4 to do this..
Your help is greatly appreciated,
Regards
Rob H
January 16, 2011 at 4:09 pm
A simple schematic of the connections would be nice. To you it is easy. I sort of found the connections to the button confusing they way you presented them.
January 17, 2011 at 12:41 am
Thanks for the feedback! I’m going to upload schematics, and include schematic screenshots with the new videos so that this is more clear. You’ll see it in the new video later today.
March 8, 2011 at 10:10 pm
I have been in the electronics field for over 40 years but for the last 6 I have been a supervisor and therefore not up to speed on the latest things. Your sight is great for those that want to learn about what is going on today not 5 years ago. Thanks for your willingness to give of your time and expertise. You also do a great job as an instructor because you cover all points and explain as you go. This is not only good for the newcomer but for those of us that have not been directly involved with design but just repair.
March 8, 2011 at 10:37 pm
Thanks! It’s great to hear that I’m reaching such a wide range of people.
March 9, 2011 at 1:44 pm
Craig is right on the money. I’m also an old-timer and now I know what all this Arduino stuff is about. Thanks,
Pingback: Must-See Beginner Tutorials For Arduino | inmotion.pt
March 10, 2011 at 7:23 am
Awesome! I recently got an arduino uno for xmas, and these tutorials have been really helpful, so thanks dude!
March 24, 2011 at 9:09 pm
Hey Jeremy,
Awesome tutorials, I’ve been looking for something like this for a long time! Now I can actually start playing around with the Arduino Uno :-D
Thank you all the way from the Canary Islands
April 5, 2011 at 10:19 pm
Hey Man,
Love the tutorials, I just finished #1 and am looking to do #2 next.
Did you know that the product you link to in the parts list for “Pushbutton” is only available in a minimum order quantity of 10,000! Do you have an alternate?
-Eric
April 6, 2011 at 8:43 pm
Oops! Thanks for pointing that out. I’ve changed the link in the parts list.
April 14, 2011 at 4:34 pm
.PDE ???
April 14, 2011 at 4:47 pm
It’s an arduino programming file. See the first tutorial for further instructions.
April 19, 2011 at 1:14 pm
Thanks from Spain for your great tutorials! :)
Pingback: [Sciguy14] Arduino Tutorial 10: Interrupts and Hardware Debouncing | Ayarafun
July 19, 2011 at 11:26 pm
Hey Jeremy,
Thanks for the great site! I am learning arduino so I can bring some products to the market and your tutorials have been my guiding light.
Quick issue (and if you don’t have time for this I completely understand)
In tutorial #2 I have been over the video many times and keep getting a compile error. ‘ledLevel was not declared in this scope.’
probably a syntax error, but I have no idea what is going wrong. So, I was wondering if 1) you would look at my code and point me to the error, 2) if you would post the text of the codes in your videos so I can work through syntax without bothering you!
Thanks again,
Wake Gregg
int switchPin = 8;
int ledPin = 11;
boolean lastButton =LOW;
boolean currentButton = LOW;
int ledLevel = 0;
void setup ()
{
pinMode (switchPin, INPUT);
pinMode (ledPin, OUTPUT);
}
boolean debounce (boolean last)
{
boolean current = digitalRead (switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop ()
{
currentButton = debounce(lastButton);
if (lastButton ==LOW && currentButton == HIGH)
{
ledLevel = ledLevel + 51;
}
lastButton = currentButton;
if (ledLevel > 255) LedLevel = 0;
analogWrite (ledPin, ledLevel);
}
July 19, 2011 at 11:57 pm
I do post all the code. There is a link to download in the post above. See if that works for you. Let me know if you are still having problems.
July 26, 2011 at 5:13 pm
Jeremy – Fantastic Job! These tutorials are great.
Portland’s Electric Bike Shop – this language is case-sensitive, so the line
if (ledLevel > 255) LedLevel = 0;
is a problem. It should be “ledLevel = 0;” not “LedLevel = 0″
Hope that helped.
Mark Petersen
January 12, 2012 at 6:49 pm
I have found your mystake:
In your last block you write:
}
lastButton = currentButton;
if (ledLevel > 255) LedLevel = 0;
analogWrite (ledPin, ledLevel);
}
If you can see you wite LedLevel (first L in uppercase) and ledLevel (first l in lowercase), I am not an expert but i have heard that the program is case sensitive.
Hello from Cartagena, Colombia
July 20, 2011 at 2:24 pm
I was viewing your videos from another website and didn’t see the files at the top of this page… Sorry about that.
Thanks again,
Wake
July 21, 2011 at 5:04 am
Hey dude, great vids, thanks!
Just to expand on the latching/debounced button thing, how hard would it be to have multiple buttons/LED’s doing this independently? I’m new to this coding stuff and can’t seem to figure out where to start with what I want to do. I can set up multiple buttons controlling multiple LED’s momentarily, and I can do the latching thing like you did in this video with one button/switch, but I can’t work out how to combine it all.
Cheers
August 7, 2011 at 8:25 pm
Hey,
thank you for your great tutorials!
But there is one thing i dont understand.
You are using a 1k or 10k ohm resistor, to connect the button to the ground. But why the current runs to the ground through the resistor and not to the digital-arduino-pin?
August 14, 2011 at 1:04 am
When the button is unpressed, the arduino pin is connected to gnd through the resistor, so it reads a logic 0. When the button is pressed, current can flow from two possible places, the 5V rail, or gnd through the resistor. The arduino detects a logic 1 when the button is pressed because the current from the 5V rail far outweighs the current that goes to ground, do to the pull down resistor.
December 18, 2011 at 12:29 pm
Hey, very interesting explanation. I did not get though the last sentence: “…5V rail far outweighs the current that goes to ground, do to the pull down resistor.” Did you mean “due to the pull down resistor”? I am not quite sure that I understand this sentence.
Best regards,
Galina
August 14, 2011 at 7:00 am
Hi, thanks for ur tutorials. I just wanted to ask what was actually that java com 23 that u used in putting the signals of 1, 0 ,1 etc….
August 14, 2011 at 4:47 pm
You mean the arduino serial terminal?
August 18, 2011 at 3:04 pm
Thanks a ton for this arduino video tutorial series!! These videos have really been helpful for a beginner like me to understand what’s going on. In this video you are using resistors, but I don’t really understand why. Can you link me to a video that provides an explanation of what resistors are used for and when to use them? That would be really helpful. Thanks!
August 20, 2011 at 2:32 pm
A resistor is used to limit current flow: http://en.wikipedia.org/wiki/Resistor
September 8, 2011 at 9:12 pm
J,
Cool site and great videos. I’m a ME but I like to tinker with some electrons and code. Thanks for all this cool stuff.
-SB
October 24, 2011 at 5:23 pm
I have a problem :( i used yours code – after mine failed (allready @ example 1).. i connected completely the same (used 10k resistor) … hm, the problem is, the led diode is on after i upload code on arduino… then i push the button and it doesn’t happen anything or the led becomes lit a bit stronger! Sometimes LED also blinks without doing anything (i have a feeling like it’s a short circuit or something isn’t connected well) – but i checked it several times, so i’m rly not sure what did i do wrong
October 28, 2011 at 10:06 pm
Can you send a picture of your circuit and your code?
November 1, 2011 at 6:28 am
@Rock ..!!
Please check whether the the legs of the switch which you are using are not already short. Actually in the 4 legged switch which JEREMY used in his tutorial – 2 legs are already shorted.
Solution to your problem – Make the connections on the diagonally opposite legs of the switch ..!!
November 5, 2011 at 9:35 am
i ‘m from india and must say these tuts are good for beginners
November 14, 2011 at 4:06 pm
Great site. Going Biomedical and we are learning Processing and the Arduino. WIll be going throught all youor tutorial. Great teaching method and preparation for all these Tutorial. Old timer as well back to school
Pingback: Learn arduino - From noob to Ninja - video series
November 28, 2011 at 5:26 pm
Thanks for the code Jeremy, it worked great though I had to make a few changes for it to work in my circuit. By the way, your website videos don’t work that well, there seems to be a buffering problem. The ones on You Tube worked just fine.
December 26, 2011 at 9:16 pm
GREAT JOB Jeremy.
I am new to the ARDUINO and learning alot from your videos.
Thanks for taking tie to teach others like myself.
January 3, 2012 at 10:54 am
Hi Jeremy, thanks for the great video!
I have a question for you or the other readers:
I want to ad 2 buttons to the circuit. In total 3 buttons.
They are the input in Pin 14, 15 and 16
Connected to pins 0, 1 and 2 is a light circuit.
I want pin 0 to give 5V if I press the button connected to pin 14,
pin 1 give 5V if I press button connected to 15
pin 2 give 5V if I press button connected to 16
I used te code from your movie and coppied it to add more inputs and outputs.
But it doesn’t work… What am I doing wrong?
This is the code:
// INPUTS
int switch01_Pin = 14;
int switch02_Pin = 15;
int switch03_Pin = 16;
// OUTPUTS
int wire01_Pin = 0;
int wire02_Pin = 1;
int wire03_Pin = 2;
// VARIABLES
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean wire01_On = false;
boolean wire02_On = false;
boolean wire03_On = false;
// VOID SETUP
void setup ()
{
pinMode(switch01_Pin, INPUT);
pinMode(switch02_Pin, INPUT);
pinMode(switch03_Pin, INPUT);
pinMode(wire01_Pin, OUTPUT);
pinMode(wire02_Pin, OUTPUT);
pinMode(wire03_Pin, OUTPUT);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switch01_Pin);
if (last != current);
boolean current = digitalRead(switch02_Pin);
if (last != current);
boolean current = digitalRead(switch03_Pin);
if (last != current);
{
delay(5);
current = digitalRead(switch01_Pin);
delay(5);
current = digitalRead(switch02_Pin);
delay(5);
current = digitalRead(switch03_Pin);
}
return current;
}
// VOID LOOP
void loop ()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH);
{
wire01_ON = !wire01_On;
wire02_ON = !wire02_On;
wire03_ON = !wire03_On;
}
lastButton = currentButton;
digitalWrite(wire01_Pin, wire01_On);
digitalWrite(wire02_Pin, wire02_On);
digitalWrite(wire03_Pin, wire03_On);
}
Thank you for your help!
January 3, 2012 at 12:01 pm
you didn’t modify the debounce function correctly. By putting “;” after the if statements, you’re making them do nothing. You need to put the content for each if statement within {} like I did in the video.
January 3, 2012 at 12:55 pm
Yeah! You are right! I remember changing this afterwards, because I thought I forgot them…
I have thee feeling that there is still something not correct in my code:
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH);
{
wire01_On = !wire01_On;
wire02_On = !wire02_On;
wire03_On = !wire03_On;
}
lastButton = currentButton;
I am debouncing one switch, then toggling all three wires. Is that correct? I want wire01 on or off if I press switch01, wire02 by switch02 and wire03 by switch03. How do I debounce the three switches?
Thanks!
January 3, 2012 at 1:45 pm
You need to debounce each one. You could modify the debounce function so that it takes the switch as an argument too, like this:
boolean debounce(boolean last, int switch)
{
boolean current = digitalRead(switch);
if (last != current)
{
delay(5);
current = digitalRead(switch);
}
return current;
}
That way, you can call it for each switch, by changing the second argument.
January 6, 2012 at 9:11 am
Hi dude,
I am a serious beginner, so you could expect a very beginner questions from me…what is it when you change from digital input to analog input. what are the differences between those two?
January 6, 2012 at 8:48 pm
Watch my TechBits episode 13. It explains all of that in detail.
January 11, 2012 at 11:57 pm
I’m trying something similar to your PWM control of an led, but with a solid state relay hooked up to a standard incandescent household light bulb (57W) that uses AC. Instead of a button, I tried to just show each dimming level for 5 seconds. It doesn’t work out too well, but instead seems to flicker on and off a few times per seconds (as far as my eye can tell). Is it just a bad idea to use PWM to control an AC device? My ultimate goal is to have a toaster oven that is PID temperature controlled so maybe PWM would have better success with a heating element since temperature changes relatively slowly? This is my test code:
int relayPin = 11;
int pwmLevel = 0;
void setup()
{
pinMode(relayPin, OUTPUT);
}
void loop()
{
pwmLevel = pwmLevel + 51;
if (pwmLevel > 255)
{
pwmLevel = 0;
}
analogWrite(relayPin, pwmLevel);
delay(5000);
}
January 14, 2012 at 5:47 pm
You shouldn’t try to control an AC line with PWM. It’s not the same as a DC signal, and it won’t work as you expect. Controlling the heating element directly makes more sense.
January 14, 2012 at 6:55 am
Currently working on to power the led and manually controlling the led with variable resistor. That part was easy. Im trying to enhance the circuit by adding two pushbuttons which function is to make the led either go dimmer or brighter. any suggestion on the codes and connection?
January 14, 2012 at 5:51 pm
Hook them up to the arduino, use my debounce code, and poll both buttons in your loop. Adjust variables that control brightness based on button state.
Pingback: Binary and Logic
January 25, 2012 at 1:44 am
Is there a way to attach multiple LEDs to a single PWM, say if I want to control more LEDs than ports I have? I’m assuming some sort of external chip that could toggle between them.
February 13, 2012 at 10:45 am
Yes, you can use a serial to parallel shift register chip.
January 25, 2012 at 5:40 pm
Is there a particular shift register chip you recommend? I found the 74HCT595 through Google.
Also, if you have the time I’d love to see a tutorial using one of the chips to control multiple multicolored LEDs!
Thanks
February 2, 2012 at 9:38 pm
I’m completely new to any sort of coding that isn’t on a TI-84 or modding Pokemon games. With your videos I’ve learned all the basics of Arduino in a couple hours. THANKS A TON!! a quick question though, the different I.D’s you make for the commands (e.g ledPin, or buttonPin etc.) do those actually matter? can i just make it like “lolPin” and “thatreallylongnamedPin”? if the DO matter, how would i use more than one of the same thing? Thanks again.
- Turner
February 19, 2012 at 8:50 am
Hey Jeremy
Great work.. Thank you
I am doing a similar project to this one but using 2 buttons to control led brightness- 1 for up 1 for down.. I am wondering how to debounce more than 1 button in the code – I cannot seem to get it to work…
Using your code Switch 4 to do this..
Your help is greatly appreciated,
Regards
Rob H