Saturday, September 4, 2021

Arduino: Lesson 4 - Read Pushbutton with digitalRead() function

Fig. 1 - Arduino Lesson 4 - Read Pushbutton with digitalRead() function

Welcome to Lesson 4 - Basic Arduino Course

Today we are going to learn how to read a Pushbutton, which when pressed, the Arduino will read and light an LED indicating that the button was pressed. We will use the digitalRead() function.

Hardware Required

  • Arduino Board
  • A Pushbutton
  • LED - optional "You can use the onboard LED"
  • 220 ohm resistor - optional "You can use the onboard resistor"

The Circuit

You can use as an example the built-in LED that most Arduino boards have. This LED is connected to a digital pin 13, however, this number may vary from board type to board type. 

This constant is LED_BUILTIN and allows you to control the built-in LED easily, and the correspondence of this constant is digital pin 13.

However, you can light an external LED with this sketch, you just need to build the same circuit as the previous lesson, Lesson 3, as we will be using the same circuit, only added from the Microswitch as shown in Figure 2 below,

Fig. 2 - Arduino Read a Microswitch with digitalRead  function - tinkercad.com

The Code

After building the circuit, connect your Arduino board to your computer, launch the Arduino Software (IDE), copy the code below and paste it into your Arduino IDE. But first, let's understand the code line by line. 

  • First we have the integer declarations, in Line 4 and in Line 5, they are integer constants, as they will not change their parameters, these are the pins that will connect the Push Button and the LED.

  • In Line 8, we create a variable that will receive the initial value "0", it will receive the state of the switch depending on whether you press it or not, it will change its value.
1
2
3
4
5
6
7
8
9
// Arduino: Lesson 4 - Read Pushbutton with digitalRead() function

  // Constants don't change.   // That's why they are used to set the pin numbers
const int buttonPin = 12;    // The number of the pushbutton pin
const int ledPin =  6;          // The number of the LED pin

// This variable will change according to the state of the LED
int buttonState = 0;            // Variable for reading the pushbutton status

  • After the declarations, we enter the in void setup() function, this function will only be read once at Arduino startup.
    Here where we declare for Arduino where we connect our peripherals. 

  • In Line 11 we use the ledPin declaration as output

  • In Line 12 we declare our buttonPin as input Pullup, its means that we are using the Arduino's internal Pullup resistor, so we don't need to use an external resistor together with the switch.
1
...
10
11
12
13
14
// Arduino: Lesson 4 - Read Pushbutton with digitalRead() function

void setup() {
pinMode(ledPin, OUTPUT); // Initialize digital pin 6 as an output.
   pinMode(buttonPin, INPUT_PULLUP);  // Initialize the pushbutton pin as an input pull up
}

  • After setting the initializations, in Line 15 we will enter the void loop() function.

  • In Line 16we get the state of the switch, when pressed, the variable gets "0", and when released, the variable gets "1".

    You may be thinking, but it shouldn't be the other way around, when you press it it goes to "1" and when I release it it goes to "0".

    Well, remember we use the Arduino's internal Pullup resistor, its means when the switch is released the Pullup resistor leaves the Arduino's gate at HIGH, "1", and when you press the switch it throws the output to the ground, which means we ground output "0" so it goes to LOW.
     
  • In Line 18, we declare a control structure, using the IF conditional, with it we are going to check if the key was pressed, if so, the variable buttonState will receive the value LOW, "0". If released, it will receive the HIGH value "1".
  • In Line 19, the digitalWrite function, command activates ledPin to HIGH level, it means that it goes from 0V to 5V, which makes the LED receive the voltage and lit up. 

  • In Line 20, we have the "else" control structure, that will be triggered if the "if" structure control isn't true.

  • In Line 21, the digitalWrite function, command activates ledPin to LOW level, it means that it goes from 5V to 0V, which makes the LED turn off.
1
...
15
16
17
18
19
20
21
22
23
// Arduino: Lesson 4 - Read Pushbutton with digitalRead() function

void loop() { // The loop function runs over and over again forever
  buttonState = digitalRead(buttonPin);   // Read the state of the pushbutton value

  if (buttonState == LOW) {  // Check if pushbutton is pressed. Case is, the buttonState is LOW
    digitalWrite(ledPin, HIGH); // Turn LED Turn On
  } else {
    digitalWrite(ledPin, LOW);  // Turn LED Turn Off
  }
}

The complete code is showed in the sketch below!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Arduino: Lesson 4 - Read Pushbutton with digitalRead() function

// Constants don't change.    // That's why they are used to set the pin numbers
const int buttonPin = 12;    // The number of the pushbutton pin
const int ledPin =  6;          // The number of the LED pin

// This variable will change according to the state of the LED
int buttonState = 0;            // Variable for reading the pushbutton status

void setup() {                                // This function is called once when the program starts
pinMode(ledPin, OUTPUT); // Initialize digital pin 6 as an output.
   pinMode(buttonPin, INPUT_PULLUP);   // Initialize the pushbutton pin as an input pull up
}

void loop() {                                      // The loop function runs over and over again forever
  buttonState = digitalRead(buttonPin);   // Read the state of the pushbutton value

  if (buttonState == HIGH) {                     // Check if pushbutton is pressed. Case is, the buttonState is HIGH
    digitalWrite(ledPin, HIGH);                 // Turn LED Turn On
  } else {
    digitalWrite(ledPin, LOW);                  // Turn LED Turn Off
  }
}
//------------------------------------- www.elcircuits.com --------------------------------------------

Next Lesson

Previous Lesson

If you have any questions, suggestions or corrections, please leave them in the comments and we will answer them soon.

Subscribe to our blog!!! Click here - elcircuits.com!!!

My Best Regards!!!

Monday, August 30, 2021

Arduino: Lesson 3 - Blinking an LED with delay() function

Fig. 1 - Arduino: Lesson 3 - Blinking an LED with delay()

Welcome to Lesson 3 - Basic Arduino Course

Today we are going to learn how to make our first algorithm, let's learn how to use the blink() function, this function makes your Arduino blink an LED, every time determined in the programming.

Hardware Required

  • Arduino Board
  • LED - optional "You can use the onboard LED"
  • 220 ohm resistor - optional "You can use the onboard resistor"

The Circuit

You can use as an example the built-in LED that most Arduino boards have. This LED is connected to a digital pin 13, however, this number may vary from board type to board type. 

This constant is LED_BUILTIN and allows you to control the built-in LED easily, and the correspondence of this constant is digital pin 13.

However, you can to lit an external LED with this sketch, you  only need to build the circuit showing in Figure 2 below, you can connect one external LED in serie with the resistor to the digital PIN 6

Connect the positive leg of the LED (the positive leg, is the anode) to the other end of the resistor. Connect the negative leg of the LED (the negative leg,  is the cathode) to the GND

Fig. 2 - Arduino Blink LED with delay function - tinkercad.com

Resistor calculation

We need to use a resistor in series with the LED to limit the current in the LED. The value of this resistor depends on the LED we will use.

Considering that we will use an external green LED, let us analyze this situation by following the steps below:

  • The power supply comes from a digital pin on the Arduino (Vdc = 5v). 
  • The LED needs a current of 20mA to be powered. 
  • And a supply voltage of 2.5V.

You can see the voltage requirements of LED in the table in Figure 3 below, which contains the average voltage of most standard color LEDs.

Fig. 3 - Typical Led Voltage Range

Knowing that the LED power supply is 2.5V and 20mA (0.02A), following the table in Figure 3, let's put the data already obtained in the formula in Figure 4 below, and calculate:

Fig. 4 - Formula to calculate the resistance for the power supply of LED

Formula: 

R = (Vps - Vled) / Iled

R = (5 - 2,5)/(0.02)

R = 125

As we can see, the result of the calculation made resulted in a resistance of 125Ω. As 20mA is on average the threshold value of the LED, R = 125Ω will also be its threshold resistance so as not to damage the LED.

The resistor value in series with the LED can be a different value, it can be one of 150 ohms, or more, such as 220 ohms, 330 ohms; the LED will also light up with higher values but with less intensity.

The Code

After building the circuit, connect your Arduino board to your computer, launch the Arduino Software (IDE), copy the code below and paste it into your Arduino IDE. But first, let's understand the code line by line. 

  • First, in void setup() structure, digital pin 6 is initialized as output pin, as shown in line 4 below 
1
2
3
4
5
// Arduino: Lesson 3 - Blinking an LED with delay()

void setup() {                      // This function is called once when the program starts pinMode(6, OUTPUT); // initialize digital pin 6 as an output.
}
  • In the void loop() structure, in line 8, the digitalWrite function, command activates port 6 to HIGH level, it means that it goes from 0V to 5V, which makes the LED receive the voltage and lit up. 
  • In the line 9, the delay() instruction indicates that we will have 1000 milliseconds or 1 second to wait, and then enters the next step which is to turn the LED off.
1
...
7
8
9
10
// Arduino: Lesson 3 - Blinking an LED with delay()

void loop() { // The loop structure runs over and over again forever digitalWrite(6, HIGH); // initialize digital pin 6 as an output.
delay(1000);           // Wait for 1 second
...

  • In the line 12, the digitalWrite function, command activates port 6 to LOW level, it means that it goes from 5V to 0V, Then turn the LED off.
  • In the line 13, the delay() instruction indicates that we will have 1000 milliseconds or 1 second to wait, and then enters the next step which is to turn the LED off.
1
...
7
...
12
13
// Arduino: Lesson 3 - Blinking an LED with delay()

void loop() { // The loop structure runs over and over again forever
digitalWrite(6, LOW); // initialize digital pin 6 as an output.
delay(1000);           // Wait for 1 second
...

This brings the LED pin back to 0 volts, and turns the LED off. Enough time should pass between powering on and powering off, that's long enough to see the LED blink. 

Therefore, the delay() function tell the Arduino board to do nothing for 1000 milliseconds or 1 second. When you use the delay() function, nothing else happens for that amount of time. 

The complete code is showed in the sketch below.

1
2
3
4
5
6
7
8
9
10
11
12
// Arduino: Lesson 3 - Blinking an LED with delay()

void setup() {                     // This function is called once when the program starts pinMode(6, OUTPUT); // initialize digital pin 6 as an output.
}

void loop() { // The loop function runs over and over again forever
digitalWrite(6, HIGH); // Turn the LED ON (HIGH voltage level)
delay(1000);           // Wait for 1 second
digitalWrite(6, LOW); // Turn the LED OFF (LOW voltage level)
delay(1000);                 // Wait for 1 second
}
//------------------------------------- www.elcircuits.com --------------------------------------------

We can also use the same code, adding a name to pin 6 of the code above, simply assigning an integer to pin 6.

1
2
3
4
// Arduino: Lesson 3 - Blinking an LED with delay()

int ledPin =  6;      // the number of the LED pin

In this way, you will use pin 6 identification as ledPin, as we can see in the complete code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Arduino: Lesson 3 - Blinking an LED with delay()

int ledPin 6;      // The number of the LED pin

void
setup() {                              // This function is called once when the program starts pinMode(ledPin, OUTPUT);
// initialize digital pin 6 "ledPin" as an output.
}

void loop() { // The loop function runs over and over again forever
digitalWrite(ledPin, HIGH); // Turn the LED ON (HIGH voltage level)
delay(1000);           // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn the LED OFF (LOW voltage level)
delay(1000);                           // Wait for 1 second
}
//------------------------------------- www.elcircuits.com --------------------------------------------

This second code will work the same way, however when we have many "Pins" ports in a larger project, and we need to change the pin to another port, in this second code, you simply change line 3 of the code, which assigns port 6, the integer ledPin to another port you need.

Next Lesson

Previous Lesson

If you have any questions, suggestions or corrections, please leave them in the comments and we will answer them soon.

Subscribe to our blog!!! Click here - elcircuits.com!!!

My Best Regards!!!

Sunday, August 22, 2021

Arduino: Lesson 2 - How to Install Arduino Software (IDE) on Windows - Step by Step!


Fig. 1 - Arduino: Lesson 2 - How to Install Arduino Software (IDE) on Windows - Step by Step!

Welcome to Lesson 2 - Basic Arduino Course

Today we are going to explain how to install Arduino Software (IDE) on Windows machines step by step. Let's get started!

STEP 1: First, download the latest version of the software here.
First of all, let's download the Arduino IDE from the official website arduino.cc, it's recommended to  get the latest version from the clicking on Download Page

Fig. 2 - How to Install Arduino IDE - Download Page

You can choose between the Installer (.exe) and the Zip packages how showing in Figure 2. We suggest you use the first one that installs directly everything you need to use the Arduino Software (IDE), including the drivers. 
Note: With the Zip package you need to install the drivers manually. The Zip file is also useful if you want to create a portable installation.

STEP 2: When the download has been completed, open the installer file and proceed with the installation, allow the driver installation process when you get a warning from the operating system and accept the License Agreement, how to showing in Figure 3 below.

Fig. 3 - How to Install Arduino IDE - Agreement Page

STEP 3: Select the folder where you want to install Arduino IDE, how to showing in Figure 4 below.

Fig. 4 - How to Install Arduino IDE - Folder location Select Page

STEP 4Choose the components to install and select all the components including the board drivers, how showing in Figure 5.
 
Fig. 5 - How to Install Arduino IDE - Check components Page

STEP 5: In some cases, we may get a “Windows Security” windows, how to showing in Figure 6 below, to install devices software, simply click on button “Install“, and wait until the installation completes.

Fig. 6 - How to Install Arduino IDE - Security Device Page

STEP 6: The process will extract and install all the required files to execute properly the Arduino Software (IDE). When finished, click in "Close" button, and the installation is finished.

Fig. 7 - How to Install Arduino IDE - Installation Page

That's it, you've just successfully installed the Arduino IDE Software on your computer!

If you have any questions, suggestions or corrections, please leave them in the comments and we will answer them soon.

Subscribe to our blog!!! Click here - elcircuits.com!!!

My Best Regards!!!