Arduino Beginners Course – Lesson 3

Arduino Beginners Course – Lesson 3

Hi! I hope that you are learning a lot with our Arduino Beginners Course! Now we are at the Lesson 3 and we are going to use some of the most common components!

What are we going to explain?

  • LEDs
  • Buttons
  • Buzzers

You will also learn what is buttons debounce with Arduino and how to write a buttons debounce with Arduino sketch!

Leds

Leds are one of the most common component and it is also one of the easiest component to control with Arduino!

You can follow this schematic. Be careful! You have to wire the shorter “leg” to the GND.

Led arduino schematic

Now we can make a simple sketch.

const int ledPin = 13;

void setup() 
{
  pinMode(ledPin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(ledPin, HIGH);  //turn the LED on (HIGH is the voltage level)
  delay(1000);                       //wait for a second
  digitalWrite(ledPin, LOW);    //turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

The LED will turn on with an HIGH and will turn off with a LOW,

You will see the LED that blinks. That’s all about LEDs…too easy? Yes maybe, but they will make awesome effects on your projects!

Buttons

You can follow this Schematic for Buttons

arduino button schematic

Now we will upload a sketch that will turn on the LED if you press the button, and turn of if you don’t press it. (we will use the built in LED but if you want you can make a new circuit adding the LED)

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int numPress = 0;            // we want to know how many times we press the button

void setup()
{
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    numPress++;
  } 
  else
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
  Serial.print("You pressed the button ");
  Serial.print(numPress);
  Serial.println(" times");
}

So you will see the LED turning on and off, but now we want to know how many times we press the button. So for example press the button 5 times, then open the Serial Monitor and you will probably read a different number. That’s because when you press a button, it often generate spurious open/close transitions due to mechanical and physical issues, these transitions may be read as multiple presses in a very short time.

So how can we avoid this problem?

We have to make a debounce code. Here it is it.

Button Debounce with Arduino:

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

int numPress = 0;            // we want to know how many times we press the button


void setup()
{
  Serial.begin(9600);

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop()
{
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay)
  {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState)
    {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH)
      {
        ledState = !ledState;
        numPress++;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
  Serial.print("You pressed the button ");
  Serial.print(numPress);
  Serial.println(" times");
}

Now you will read the correct number thank to this buttons debounce with Arduino.

Buzzer

Another very very easy component is the buzzer. Here it is a Schematic for it but I suggest to follow this Tutorial -> Star Wars Theme with a Buzzer

You will learn how to use a buzzer with a pretty project!

arduino buzzer schematic

That’s all for this Lesson 3 of the Arduino Beginners Course!

Enable notification pressing on the bell on the right so that you will receive a simple notification when we publish a new post!

Morover follow us on Instagram and Twitter!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: