Arquivo de Arduino Mini Course - Electronic Circuits https://www.elcircuits.com/category/embedded-system/arduino-mini-course/ Circuits, tips, projects, and electronics tutorials for beginners and enthusiasts. Fri, 27 Feb 2026 18:27:55 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://www.elcircuits.com/wp-content/uploads/2025/11/cropped-LOGO-ELC-500x500px-elcircuits.com_-1-32x32.png Arquivo de Arduino Mini Course - Electronic Circuits https://www.elcircuits.com/category/embedded-system/arduino-mini-course/ 32 32 Arduino Lesson 9 – LED Sequencing with Arrays (Practical Guide) https://www.elcircuits.com/arduino-lesson-9-led-sequencing-arrays/ https://www.elcircuits.com/arduino-lesson-9-led-sequencing-arrays/#respond Sun, 07 Nov 2021 00:08:00 +0000 https://elcircuits.com/arduino-lesson-9-led-sequencing-using-array-data-structure/ Arduino: Lesson 9 – LED Sequencing using Array Data Structure Welcome to Lesson 9 – Basic Arduino Course In today’s lesson, we will learn how to use 5 LEDs connected to 5 Arduino ports, which will light up in sequence and right after the cycle ends, will go out in sequence. With that, we will learn a new instruction, the Array data structure. What is Array? An array is a collection of variables that are accessed with an index number. However, the “array” data structure is not exactly an existing data type. There are arrays of variables like “char“, “int“, “boolean“, “float” and so on. This means that an array can be variables of any type mentioned above. An array, orvector, for example, is a collection of variables of a specific type, which may even use the same nomenclature, but which will be distinguished by an indexed number. For example: We can use a variable type “float” with a single name indexed by number, instead of different variables (VarFloat_1, VarFloat_2, VarFloat_3…) we can use a single grouped array with the same nomenclature that will allow each variable be manipulated separately by a numerical index, (VarFloat [0], VarFloat [1], VarFloat [2] …). Hardware Required Arduino Board 5 – LEDs 5 – 250 ohms resistor – (brown, green, brown, gold) Jumper Wires Protoboard (optional) The Circuit Connections The circuit is a bit simple, we connect the longer “Anode” legs of the 5 LEDs to the positive 5V of the Arduino, and the other shorter leg “Cathode“, we connect to the 150 ohm resistor and in series with GND, negative of the Arduino, as shown in Figure 2 below. Fig. 2 – LED Sequencing using Array Data Structure – tinkercad.com We use a protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino. The Code The code can be implemented in the normal way in programming, as shown in the code below. But the code is quite large, we use 42 lines of code to create this kind of sequential LED circuit. To write less lines of code, an “optimizer” is required. // Arduino: Lesson 9 - LED Sequencing using Array Data Structure const int Led1 = 2; // Select the output LED1 PIN const int Led2 = 3; // Select the output LED2 PIN const int Led3 = 4; // Select the output LED3 PIN const int Led4 = 5; // Select the output LED4 PIN const int Led5 = 6; // Select the output LED5 PIN void setup() { // This function is called once when the program starts pinMode(Led1, OUTPUT); // Initialize digital pin LED1 as an output. pinMode(Led2, OUTPUT); // Initialize digital pin LED2 as an output. pinMode(Led3, OUTPUT); // Initialize digital pin LED3 as an output. pinMode(Led4, OUTPUT); // Initialize digital pin LED4 as an output. pinMode(Led5, OUTPUT); // Initialize digital pin LED5 as an output. } void loop() { // The loop function runs over and over again as long as the Arduino has power. // Turn ON each LED sequence in order digitalWrite(Led1, HIGH); delay(150); // Wait for 150 millisecond(s) digitalWrite(Led2, HIGH); delay(150); // Wait for 150 millisecond(s) digitalWrite(Led3, HIGH); delay(150); // Wait for 150 millisecond(s) digitalWrite(Led4, HIGH); delay(150); // Wait for 150 millisecond(s) digitalWrite(Led5, HIGH); delay(150); // Wait for 150 millisecond(s) // Turn OFF each LED sequence in order digitalWrite(Led1, LOW); delay(150); // Wait for 150 millisecond(s) digitalWrite(Led2, LOW); delay(150); // Wait for 150 millisecond(s) digitalWrite(Led3, LOW); delay(150); // Wait for 150 millisecond(s) digitalWrite(Led4, LOW); delay(150); // Wait for 150 millisecond(s) digitalWrite(Led5, LOW); delay(150); // Wait for 150 millisecond(s) } //------------------------------------- www.elcircuits.com -------------------------------------------- This is where the Array data structure comes into play, we can use it to drastically reduce the number of lines of code, as shown in the code example below. // Arduino: Lesson 9 - LED Sequencing using Array Data Structure const int Leds = 5; //Number of LEDs in the circuit const int PinLeds[Leds] = {2, 3, 4, 5, 6}; //LEDs connected to pins 2 to 6 void setup() { // This function is called once when the program starts for (int i = 0; i < Leds; i++) { // Go through each of the elements in our array pinMode(PinLeds[i], OUTPUT); // And set them as OUTPUT } } void loop() { // The loop function runs over and over again as long as the Arduino has power. for(int i = 0; i < Leds; i++) { // Go through each of the Leds elements in our array digitalWrite(PinLeds[i], HIGH); // And set each of the PinLeds in HIGH level delay(150); // Wait 150 millis seconds, and back to structure For } for(int i = 0; i < Leds; i++) { // Go through each of the Leds elements in our array digitalWrite(PinLeds[i], LOW); // And set each of the PinLeds in LOW level delay(150); // Wait 150 millis seconds, and back to structure For } } //------------------------------------- www.elcircuits.com -------------------------------------------- Using the Array Data Structure technique, we could practically save half a line of the previous code, that's really cool, right? After building the circuit, connect your Arduino board to your computer, run the Arduino software (IDE), copy the code below and paste it into your Arduino IDE. But first let us understand the code line by line. In Line 3, we declared the number of LEDs in the circuit. In Line 4, We are using an Array to index each pin of the LEDs in the corresponding Arduino Ports. In Line 6, we enter the void setup() function. This function is read only once when the Arduino is started. In Line 7, we enter in the FOR structure control, to access each Leds element. in Line 8, we set each PinLeds as OUTPUT, through each elements in ou array. In Line 11, we enter in the loop() function does precisely what its name suggests, and loops consecutively. In Line 12, we enter in the For structure control, to go through each of

O post Arduino Lesson 9 – LED Sequencing with Arrays (Practical Guide) apareceu primeiro em Electronic Circuits.

]]>
Arduino: Lesson 9 - LED Sequencing using Array Data Structure

Arduino: Lesson 9 – LED Sequencing using Array Data Structure

Welcome to Lesson 9 – Basic Arduino Course

In today’s lesson, we will learn how to use 5 LEDs connected to 5 Arduino ports, which will light up in sequence and right after the cycle ends, will go out in sequence. With that, we will learn a new instruction, the Array data structure.

What is Array?

An array is a collection of variables that are accessed with an index number. However, the “arraydata structure is not exactly an existing data type.

There are arrays of variables like “char“, “int“, “boolean“, “float” and so on. This means that an array can be variables of any type mentioned above.

An array, orvector, for example, is a collection of variables of a specific type, which may even use the same nomenclature, but which will be distinguished by an indexed number.

For example: We can use a variable type “float” with a single name indexed by number, instead of different variables (VarFloat_1, VarFloat_2, VarFloat_3…) we can use a single grouped array with the same nomenclature that will allow each variable be manipulated separately by a numerical index, (VarFloat [0], VarFloat [1], VarFloat [2] …).

Hardware Required

  • Arduino Board
  • 5 – LEDs
  • 5 – 250 ohms resistor – (brown, green, brown, gold)
  • Jumper Wires
  • Protoboard (optional)

The Circuit Connections

The circuit is a bit simple, we connect the longer “Anode” legs of the 5 LEDs to the positive 5V of the Arduino, and the other shorter leg “Cathode“, we connect to the 150 ohm resistor and in series with GND, negative of the Arduino, as shown in Figure 2 below.

Fig. 2 - LED Sequencing using Array Data Structure - tinkercad.com

Fig. 2 – LED Sequencing using Array Data Structure – tinkercad.com

We use a protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino.

The Code

The code can be implemented in the normal way in programming, as shown in the code below. But the code is quite large, we use 42 lines of code to create this kind of sequential LED circuit. To write less lines of code, an “optimizer” is required.

// Arduino: Lesson 9 - LED Sequencing using Array Data Structure
const int Led1 =  2;     // Select the output LED1 PIN
const int Led2 =  3;     // Select the output LED2 PIN
const int Led3 =  4;     // Select the output LED3 PIN
const int Led4 =  5;     // Select the output LED4 PIN
const int Led5 =  6;     // Select the output LED5 PIN
void setup() {            // This function is called once when the program starts
  pinMode(Led1, OUTPUT);  // Initialize digital pin LED1 as an output.
  pinMode(Led2, OUTPUT);  // Initialize digital pin LED2 as an output.
  pinMode(Led3, OUTPUT);  // Initialize digital pin LED3 as an output.
  pinMode(Led4, OUTPUT);  // Initialize digital pin LED4 as an output.
  pinMode(Led5, OUTPUT);  // Initialize digital pin LED5 as an output.
}
void loop() {                           // The loop function runs over and over again as long as the Arduino has power.
  // Turn ON each LED sequence in order
  digitalWrite(Led1, HIGH);
  delay(150);               // Wait for 150 millisecond(s)
  digitalWrite(Led2, HIGH);
  delay(150);               // Wait for 150 millisecond(s)
  digitalWrite(Led3, HIGH);
  delay(150);               // Wait for 150 millisecond(s)
  digitalWrite(Led4, HIGH);
  delay(150);               // Wait for 150 millisecond(s)
  digitalWrite(Led5, HIGH);
  delay(150);               // Wait for 150 millisecond(s)
  // Turn OFF each LED sequence in order
  digitalWrite(Led1, LOW);
  delay(150);               // Wait for 150 millisecond(s)
  digitalWrite(Led2, LOW);
  delay(150);               // Wait for 150 millisecond(s)
  digitalWrite(Led3, LOW);
  delay(150);               // Wait for 150 millisecond(s)
  digitalWrite(Led4, LOW);
  delay(150);               // Wait for 150 millisecond(s)
  digitalWrite(Led5, LOW);
  delay(150);               // Wait for 150 millisecond(s)
}
//------------------------------------- www.elcircuits.com --------------------------------------------

This is where the Array data structure comes into play, we can use it to drastically reduce the number of lines of code, as shown in the code example below.

// Arduino: Lesson 9 - LED Sequencing using Array Data Structure
const int Leds = 5;                          //Number of LEDs in the circuit
const int PinLeds[Leds] = {2, 3, 4, 5, 6};   //LEDs connected to pins 2 to 6
void setup() {            // This function is called once when the program starts
  for (int i = 0; i < Leds; i++) {    // Go through each of the elements in our array
    pinMode(PinLeds[i], OUTPUT);  // And set them as OUTPUT
  }
}
void loop() {                                        // The loop function runs over and over again as long as the Arduino has power.
  for(int i = 0; i < Leds; i++) {    // Go through each of the Leds elements in our array
    digitalWrite(PinLeds[i], HIGH);   // And set each of the PinLeds in HIGH level
    delay(150);   // Wait 150 millis seconds, and back to structure For
  }
  for(int i = 0; i < Leds; i++) {    // Go through each of the Leds elements in our array
    digitalWrite(PinLeds[i], LOW);   // And set each of the PinLeds in LOW level
    delay(150);   // Wait 150 millis seconds, and back to structure For
  }
}
//------------------------------------- www.elcircuits.com --------------------------------------------

Using the Array Data Structure technique, we could practically save half a line of the previous code, that's really cool, right?

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

  • In Line 3, we declared the number of LEDs in the circuit.
  • In Line 4, We are using an Array to index each pin of the LEDs in the corresponding Arduino Ports.
  • In Line 6, we enter the void setup() function. This function is read only once when the Arduino is started.
  • In Line 7, we enter in the FOR structure control, to access each Leds element.
  • in Line 8, we set each PinLeds as OUTPUT, through each elements in ou array.
  • In Line 11, we enter in the loop() function does precisely what its name suggests, and loops consecutively.
  • In Line 12, we enter in the For structure control, to go through each of the Leds elements in our array.
  • In Line 13, we continue in the For structure control, and set each of the PinLeds in HIGH level, turn on Led by Led.
  • In Line 14, we enter in the delay function, to wait 150 millis seconds, and back to structure For while it is true.
  • In Line 16, we enter in the For structure control, to go through each of the Leds elements in our array.
  • In Line 17, we continue in the For structure control, and set each of the PinLeds in LOW level, turn off Led by Led.
  • In Line 18, we enter in the delay function, to wait 150 millis seconds, and back to structure For while it is true.

All ready! After you have assembled the entire circuit, and uploaded this code, what you should see is the sequence of LEDs turning on and off, giving the impression that the LEDs are moving forward.

Next Lesson

  • Arduino: Lesson 10 - How to Read Temperature and Humidity with Arduino, Using the DHT11 Sensor

Previous Lesson


✨ Our Gratitude and Next Steps


We sincerely hope this guide has been useful and enriching for your
projects! Thank you for dedicating your time to this content.


Your Feedback is Invaluable:


Have any questions, suggestions, or corrections? Feel free to share them
in the comments below! Your contribution helps us refine this
content for the entire ElCircuits community.


If you found this guide helpful, spread the knowledge!


🔗 Share This
Guide


Best regards,

The ElCircuits Team ⚡

O post Arduino Lesson 9 – LED Sequencing with Arrays (Practical Guide) apareceu primeiro em Electronic Circuits.

]]>
https://www.elcircuits.com/arduino-lesson-9-led-sequencing-arrays/feed/ 0
Arduino Lesson 8 – Using LDR Sensor and Serial Monitor Readings https://www.elcircuits.com/arduino-lesson-8-ldr-sensor-serial-monitor/ https://www.elcircuits.com/arduino-lesson-8-ldr-sensor-serial-monitor/#respond Sun, 17 Oct 2021 14:37:00 +0000 https://elcircuits.com/arduino-lesson-8-using-ldr-sensor-and-reading-values-on-serial-monitor/ Using LDR Sensor and reading values on Serial Monitor Welcome to Lesson 8 – Basic Arduino Course In today’s lesson, we will learn how to use an LDR sensor and read values on a Serial Monitor with Arduino. To measure light intensity, we will use the famous and widely used low-cost LDR (Light Dependent Resistor) sensor, to detect the intensity of light or darkness easily and cheaply. LDR (Light Dependent Resistor) Sensor LDR is a special type of resistor that passes higher voltage (low resistance) when light intensity is high and low voltage (high resistance) when darkness is low. With this method, we can use it for example in projects such as: Lack of Light Alarm – Triggers an alarm when power is cut. Brightness Control – Used to control the brightness of a recording or filming environment for example. Contrast Control Screen – Often used on mobile phones to automatically lower or increase the brightness of the Display. Emergency Light – Automatically turns on when Light is cut. Ticket Counter – Also used to count people at the entrance of an establishment. Invasion Alarm – Used to trigger an alarm when there is an invasion of a monitored establishment. In today’s example, we will use this sensor to measure numerical resistance and read these values from the serial monitor. To do this, we will use Arduino analog port A0. We can use any analog port, in the case of Arduino Uno there are 6 ports; A0 to A5. Hardware Required Arduino Board LDR – Sensor Dependent Light 100K ohms resistor – (Brown, black, yellow, gold) Jumper Wires Protoboard (optional) The Circuit Connections The circuit is very simple, we connect one leg of the LDR to positive 5V, and another leg to a 100K resistor in series with negative GND, and the same leg that takes the resistor and LDR, we connect to port A0, as shown in Figure 2 below. Fig. 2 – Using LDR Sensor and reading values on Serial Monitor – tinkercad.com We use a protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino. The Code The analogRead() function reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage (5V or 3.3V) into integer values between 0 and 1023. On an Arduino UNO, for example, this yields a resolution between readings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit. After building the circuit, connect your Arduino board to your computer, launch Arduino Software (IDE), copy the code below and paste it into your Arduino IDE. But first let us understand the code line by line. In Line 3, we declared sensorPin to select the analog input Pin A0 to connect the LDR. In Line 4, we declared sensorValue as a variable to store the value coming from the LDR sensor. In Line 6, we enter the void setup() function. This function is read only once when the Arduino is started. In Line 7, we set the serial port for communication, we will read the numeric value from the LDR sensor. // Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino int sensorPin = A0; // Select the Analog input pin for LDR int sensorValue = 0; // Variable to store the value coming from the sensor void setup() { // This function is called once when the program starts Serial.begin(9600); // Sets Serial Port for communication with bounce rate in 9600 } void loop() { // The loop function runs over and over again as long as the Arduino has power. sensorValue = analogRead(sensorPin); // Read the value from the sensor Serial.println(sensorValue); // Prints the values coming from the sensor on the Serial Monitor } //------------------------------------- www.elcircuits.com -------------------------------------------- In Line 10, we enter in the loop() function does precisely what its name suggests, and loops consecutively. In Line 11, the sensorValue variable receives the value read from the sensorPin, which is the analog input Pin, and stores the read values. In Line 12, the Serial.println() function prints the values from the variable sensorValue on the serial monitor screen. Below you can see the full code, which we can be copying and pasting into your Arduino IDE, and uploading to the Arduino. // Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino int sensorPin = A0; // Select the Analog input pin for LDR int sensorValue = 0; // Variable to store the value coming from the sensor void setup() { // This function is called once when the program starts Serial.begin(9600); // Sets Serial Port for communication with bounce rate in 9600 } void loop() { // The loop function runs over and over again as long as the Arduino has power. sensorValue = analogRead(sensorPin); // Read the value from the sensor Serial.println(sensorValue); // Prints the values coming from the sensor on the Serial Monitor } //------------------------------------- www.elcircuits.com -------------------------------------------- All ready! After you have assembled the entire circuit, and uploaded this code, open the Serial Monitor and what you will see is the numerical value of the LDR. When you approach your hands to the LDR, inhibiting the light, the number will drop to the minimum possible, it will depend on the LDR, and when you shine a light on the sensor, the number will go to the maximum of the sensor. With that, the possibilities are immense to work with this sensor. Next Lesson Arduino: Lesson 9 – LED Sequencing using Array Data Structure Previous Lesson Arduino: Lesson 5 – Reading Potentiometer and Showing Values On Serial Monitor Arduino: Lesson 6 – How to use Analog Output to fade an LED Arduino: Lesson 7 – Controlling LED Through Serial Monitor with Arduino ✨ Our Gratitude and Next Steps We sincerely hope this guide has been useful and enriching for

O post Arduino Lesson 8 – Using LDR Sensor and Serial Monitor Readings apareceu primeiro em Electronic Circuits.

]]>
Fig. 1 - Using LDR Sensor and reading values on Serial Monitor

Using LDR Sensor and reading values on Serial Monitor

Welcome to Lesson 8 – Basic Arduino Course

In today’s lesson, we will learn how to use an LDR sensor and read values on a Serial Monitor with Arduino.

To measure light intensity, we will use the famous and widely used low-cost LDR (Light Dependent Resistor) sensor, to detect the intensity of light or darkness easily and cheaply.

LDR (Light Dependent Resistor) Sensor

LDR is a special type of resistor that passes higher voltage (low resistance) when light intensity is high and low voltage (high resistance) when darkness is low.

With this method, we can use it for example in projects such as:

  • Lack of Light Alarm – Triggers an alarm when power is cut.
  • Brightness Control – Used to control the brightness of a recording or filming environment for example.
  • Contrast Control Screen – Often used on mobile phones to automatically lower or increase the brightness of the Display.
  • Emergency Light – Automatically turns on when Light is cut.
  • Ticket Counter – Also used to count people at the entrance of an establishment.
  • Invasion Alarm – Used to trigger an alarm when there is an invasion of a monitored establishment.

In today’s example, we will use this sensor to measure numerical resistance and read these values from the serial monitor.

To do this, we will use Arduino analog port A0. We can use any analog port, in the case of Arduino Uno there are 6 ports; A0 to A5.

Hardware Required

  • Arduino Board
  • LDR – Sensor Dependent Light
  • 100K ohms resistor – (Brown, black, yellow, gold)
  • Jumper Wires
  • Protoboard (optional)

The Circuit Connections

The circuit is very simple, we connect one leg of the LDR to positive 5V, and another leg to a 100K resistor in series with negative GND, and the same leg that takes the resistor and LDR, we connect to port A0, as shown in Figure 2 below.

Fig. 2 - Using LDR Sensor and reading values on Serial Monitor - tinkercad.com

Fig. 2 – Using LDR Sensor and reading values on Serial Monitor – tinkercad.com

We use a protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino.

The Code

The analogRead() function reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage (5V or 3.3V) into integer values between 0 and 1023.

On an Arduino UNO, for example, this yields a resolution between readings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit.

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

  • In Line 3, we declared sensorPin to select the analog input Pin A0 to connect the LDR.
  • In Line 4, we declared sensorValue as a variable to store the value coming from the LDR sensor.
  • In Line 6, we enter the void setup() function. This function is read only once when the Arduino is started.
  • In Line 7, we set the serial port for communication, we will read the numeric value from the LDR sensor.
// Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino

int sensorPin = A0;      // Select the Analog input pin for LDR
int sensorValue = 0;       // Variable to store the value coming from the sensor

void setup() {            // This function is called once when the program starts
  Serial.begin(9600);   // Sets Serial Port for communication with bounce rate in 9600
}

void loop() {                                           // The loop function runs over and over again as long as the Arduino has power.
  sensorValue = analogRead(sensorPin);   // Read the value from the sensor
  Serial.println(sensorValue);             // Prints the values coming from the sensor on the Serial Monitor
}
//------------------------------------- www.elcircuits.com --------------------------------------------
  • In Line 10, we enter in the loop() function does precisely what its name suggests, and loops consecutively.
  • In Line 11, the sensorValue variable receives the value read from the sensorPin, which is the analog input Pin, and stores the read values.
  • In Line 12, the Serial.println() function prints the values from the variable sensorValue on the serial monitor screen.

Below you can see the full code, which we can be copying and pasting into your Arduino IDE, and uploading to the Arduino.

// Arduino Lesson 8 - Using LDR Sensor and reading values on Serial Monitor with Arduino

int sensorPin = A0;      // Select the Analog input pin for LDR
int sensorValue = 0;       // Variable to store the value coming from the sensor

void setup() {            // This function is called once when the program starts
  Serial.begin(9600);   // Sets Serial Port for communication with bounce rate in 9600
}

void loop() {                                           // The loop function runs over and over again as long as the Arduino has power.
  sensorValue = analogRead(sensorPin);   // Read the value from the sensor
  Serial.println(sensorValue);             // Prints the values coming from the sensor on the Serial Monitor
}
//------------------------------------- www.elcircuits.com --------------------------------------------

All ready! After you have assembled the entire circuit, and uploaded this code, open the Serial Monitor and what you will see is the numerical value of the LDR.

When you approach your hands to the LDR, inhibiting the light, the number will drop to the minimum possible, it will depend on the LDR, and when you shine a light on the sensor, the number will go to the maximum of the sensor.

With that, the possibilities are immense to work with this sensor.

Next Lesson

Previous Lesson

✨ Our Gratitude and Next Steps

We sincerely hope this guide has been useful and enriching for your projects! Thank you for dedicating your time to this content.

Your Feedback is Invaluable:

Have any questions, suggestions, or corrections? Feel free to share them in the comments below! Your contribution helps us refine this content for the entire ElCircuits community.

If you found this guide helpful, spread the knowledge!

🔗 Share This Guide

Best regards,
The ElCircuits Team ⚡

O post Arduino Lesson 8 – Using LDR Sensor and Serial Monitor Readings apareceu primeiro em Electronic Circuits.

]]>
https://www.elcircuits.com/arduino-lesson-8-ldr-sensor-serial-monitor/feed/ 0
Arduino Lesson 7 – Controlling LED via Serial Monitor https://www.elcircuits.com/arduino-lesson-7-led-control-serial-monitor/ https://www.elcircuits.com/arduino-lesson-7-led-control-serial-monitor/#respond Sun, 10 Oct 2021 23:39:00 +0000 https://elcircuits.com/arduino-lesson-7-controlling-led-through-serial-monitor-with-arduino/ Arduino: Lesson 7 – Controlling LED Through Serial Monitor with Arduino Welcome to Lesson 7 – Basic Arduino Course In today’s lesson, we will learn how to control the state of a LED via the Serial Monitor using the Arduino IDE. We will use the Serial.readString() function which will cause the Arduino to interpret the sentence you have entered into the serial monitor, e.g. “Turn on the led“. If you press ENTER Key in your computer, the LED will be turned on, if you want to turn off the LED just write the message “Turn off the LED“. With this method we can use it in different projects such as: Load activation – Turns one or more loads on or off using commands. Motor Speed Control – Use Serial Monitor to send the speed at which a PWM motor should run. Calibrate a sensor’s constant – We can create software to change a sensor’s constant to a specified value if needed. In our example today, we are using the serial monitor to trigger a LED, but you can also use a relay module to drive a motor, for example. To do this, we will use the D9 digital port on Arduino. We can use any digital port we want. Just change the port you want to use in the declaration or the one that is available on your Arduino. Required hardware Arduino board LED – Light Emitter Diode 220 Ohm Resistor – (red, red, brown, gold) Jumper wires Protoboard (optional) The circuit The circuit is quite simple. We connect a LED in series with a 220 ohm resistor to limit the current in the LED as we learned in previous lessons, and we connect the 9-pin of the Arduino UNO as shown in Figure 2 below. Fig. 2 – Controlling LED Through Serial Monitor with Arduino – tinkercad.com We use a protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino. The code The Serial.readString() function reads characters from the serial buffer and moves them to a given string. In our example, we want to do something very simple, which is to turn on and off a LED using the command from Serial Monitor. After building the circuit, connect your Arduino board to your computer, run the Arduino software (IDE), copy the code below and paste it into your Arduino IDE. But first let us understand the code line by line. // Arduino: Lesson 7 - Controlling LED Through Serial Monitor with Arduino int ledPin = 9; // LED connected to digital pin 9 String DataIn; // String that will receive the Serial Monitor commands void setup() { // This function is read only once when the Arduino is started Serial.begin(115200); // Begin Serial Monitor with bounce rate in 115200 pinMode(ledPin, OUTPUT); // Set the digital pin as output: } void loop() { // The loop function runs over and over again as long as the Arduino has power. if (Serial.available()) { // Check if there is any data on the serial monitor DataIn = Serial.readString(); // String DataIn receives the data typed in the Serial Monitor } if (DataIn == "turn led on") { // Check if the received String is equal to "turn led on" digitalWrite(ledPin, HIGH); // If yes, the function digitalWrite turns Led ON } if (DataIn == "turn led off") { // Check if the received String is equal to "turn led off" digitalWrite(ledPin, LOW); // If yes, the function digitalWrite turns Led OFF } } //------------------------------------- www.elcircuits.com -------------------------------------------- In Line 11, we enter in the void loop() function does precisely what its name suggests, and loops consecutively. In Line 12, we enter in a if conditional, for to check if the Serial Monitor is available, if yes we call the next function. In Line 13, we call the Serial.readString() function to read the characters from the Serial Monitor and send them to the String DataIn. In Line 15, we enter in a if conditional, in this case to compare if the characters are the same as written in the Serial Monitor, in our example “turn led on“, if yes… In Line 16, we enter the digitalWrite function, command activates ledPin to HIGH level, it means that it goes from 0V to 5V, which turn the LED On. In Line 17, we enter in a if conditional, that compare if the characters are the same as written in the Serial Monitor, in our example “turn led off“, if yes… In Line 18, we enter the digitalWrite function, command disables ledPin to LOW level, it means that it goes from 5V to 0V, which turn the LED Off. // Arduino: Lesson 7 - Controlling LED Through Serial Monitor with Arduino int ledPin = 9; // LED connected to digital pin 9 String DataIn; // String that will receive the Serial Monitor commands void setup() { // This function is read only once when the Arduino is started Serial.begin(115200); // Begin Serial Monitor with bounce rate in 115200 pinMode(ledPin, OUTPUT); // Set the digital pin as output: } void loop() { // The loop function runs over and over again as long as the Arduino has power. if (Serial.available()) { // Check if there is any data on the serial monitor DataIn = Serial.readString(); // String DataIn receives the data typed in the Serial Monitor } if (DataIn == "turn led on") { // Check if the received String is equal to "turn led on" digitalWrite(ledPin, HIGH); // If yes, the function digitalWrite turns Led ON } if (DataIn == "turn led off") { // Check if the received String is equal to "turn led off" digitalWrite(ledPin, LOW); // If yes, the function digitalWrite turns Led OFF } } //------------------------------------- www.elcircuits.com -------------------------------------------- Below you can see the full code, which we can be copying and pasting into your Arduino IDE, and uploading to Arduino. The complete code is showed in the sketch below! // Arduino: Lesson 7 - Controlling LED

O post Arduino Lesson 7 – Controlling LED via Serial Monitor apareceu primeiro em Electronic Circuits.

]]>
Fig 1 - Arduino: Lesson 7 - Controlling LED Through Serial Monitor with Arduino

Arduino: Lesson 7 – Controlling LED Through Serial Monitor with Arduino

Welcome to Lesson 7 – Basic Arduino Course

In today’s lesson, we will learn how to control the state of a LED via the Serial Monitor using the Arduino IDE.

We will use the Serial.readString() function which will cause the Arduino to interpret the sentence you have entered into the serial monitor, e.g. “Turn on the led“. If you press ENTER Key in your computer, the LED will be turned on, if you want to turn off the LED just write the message “Turn off the LED“.

With this method we can use it in different projects such as:

  • Load activation – Turns one or more loads on or off using commands.
  • Motor Speed Control – Use Serial Monitor to send the speed at which a PWM motor should run.
  • Calibrate a sensor’s constant – We can create software to change a sensor’s constant to a specified value if needed.

In our example today, we are using the serial monitor to trigger a LED, but you can also use a relay module to drive a motor, for example.

To do this, we will use the D9 digital port on Arduino. We can use any digital port we want. Just change the port you want to use in the declaration or the one that is available on your Arduino.

Required hardware

  • Arduino board
  • LED – Light Emitter Diode 220 Ohm Resistor – (red, red, brown, gold)
  • Jumper wires
  • Protoboard (optional)

The circuit

The circuit is quite simple. We connect a LED in series with a 220 ohm resistor to limit the current in the LED as we learned in previous lessons, and we connect the 9-pin of the Arduino UNO as shown in Figure 2 below.

Fig. 2 - Controlling LED Through Serial Monitor with Arduino - tinkercad.com

Fig. 2 – Controlling LED Through Serial Monitor with Arduino – tinkercad.com

We use a protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino.

The code

The Serial.readString() function reads characters from the serial buffer and moves them to a given string.

In our example, we want to do something very simple, which is to turn on and off a LED using the command from Serial Monitor.

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

// Arduino: Lesson 7 - Controlling LED Through Serial Monitor with Arduino

int ledPin = 9;      // LED connected to digital pin 9
String DataIn;       // String that will receive the Serial Monitor commands

void setup() {            // This function is read only once when the Arduino is started
  Serial.begin(115200);   // Begin Serial Monitor with bounce rate in 115200
  pinMode(ledPin, OUTPUT);  // Set the digital pin as output:
}

void loop() {                                           // The loop function runs over and over again as long as the Arduino has power.
  if (Serial.available()) {  // Check if there is any data on the serial monitor
    DataIn = Serial.readString();  // String DataIn receives the data typed in the Serial Monitor
  }
  if (DataIn == "turn led on") {  // Check if the received String is equal to "turn led on"
    digitalWrite(ledPin, HIGH);  // If yes, the function digitalWrite turns Led ON
  }
  if (DataIn == "turn led off") {  // Check if the received String is equal to "turn led off"
    digitalWrite(ledPin, LOW);   // If yes, the function digitalWrite turns Led OFF
  }
}
//------------------------------------- www.elcircuits.com --------------------------------------------
  • In Line 11, we enter in the void loop() function does precisely what its name suggests, and loops consecutively.
  • In Line 12, we enter in a if conditional, for to check if the Serial Monitor is available, if yes we call the next function.
  • In Line 13, we call the Serial.readString() function to read the characters from the Serial Monitor and send them to the String DataIn.
  • In Line 15, we enter in a if conditional, in this case to compare if the characters are the same as written in the Serial Monitor, in our example “turn led on“, if yes…
  • In Line 16, we enter the digitalWrite function, command activates ledPin to HIGH level, it means that it goes from 0V to 5V, which turn the LED On.
  • In Line 17, we enter in a if conditional, that compare if the characters are the same as written in the Serial Monitor, in our example “turn led off“, if yes…
  • In Line 18, we enter the digitalWrite function, command disables ledPin to LOW level, it means that it goes from 5V to 0V, which turn the LED Off.
// Arduino: Lesson 7 - Controlling LED Through Serial Monitor with Arduino

int ledPin = 9;      // LED connected to digital pin 9
String DataIn;       // String that will receive the Serial Monitor commands

void setup() {            // This function is read only once when the Arduino is started
  Serial.begin(115200);   // Begin Serial Monitor with bounce rate in 115200
  pinMode(ledPin, OUTPUT);  // Set the digital pin as output:
}

void loop() {                                           // The loop function runs over and over again as long as the Arduino has power.
  if (Serial.available()) {  // Check if there is any data on the serial monitor
    DataIn = Serial.readString();  // String DataIn receives the data typed in the Serial Monitor
  }
  if (DataIn == "turn led on") {  // Check if the received String is equal to "turn led on"
    digitalWrite(ledPin, HIGH);  // If yes, the function digitalWrite turns Led ON
  }
  if (DataIn == "turn led off") {  // Check if the received String is equal to "turn led off"
    digitalWrite(ledPin, LOW);   // If yes, the function digitalWrite turns Led OFF
  }
}
//------------------------------------- www.elcircuits.com --------------------------------------------

Below you can see the full code, which we can be copying and pasting into your Arduino IDE, and uploading to Arduino.

The complete code is showed in the sketch below!

// Arduino: Lesson 7 - Controlling LED Through Serial Monitor with Arduino

int ledPin = 9;      // LED connected to digital pin 9
String DataIn;       // String that will receive the Serial Monitor commands

void setup() {            // This function is read only once when the Arduino is started
  Serial.begin(115200);   // Begin Serial Monitor with bounce rate in 115200
  pinMode(ledPin, OUTPUT);  // Set the digital pin as output:
}

void loop() {                                           // The loop function runs over and over again as long as the Arduino has power.
  if (Serial.available()) {  // Check if there is any data on the serial monitor
    DataIn = Serial.readString();  // String DataIn receives the data typed in the Serial Monitor
  }
  if (DataIn == "turn led on") {  // Check if the received String is equal to "turn led on"
    digitalWrite(ledPin, HIGH);  // If yes, the function digitalWrite turns Led ON
  }
  if (DataIn == "turn led off") {  // Check if the received String is equal to "turn led off"
    digitalWrite(ledPin, LOW);   // If yes, the function digitalWrite turns Led OFF
  }
}
//------------------------------------- www.elcircuits.com --------------------------------------------

All ready! After you have assembled the entire circuit, and uploaded this code, open the Serial Monitor and what you will see is the numerical value of the LDR.

When you approach your hands to the LDR, inhibiting the light, the number will drop to the minimum possible, it will depend on the LDR, and when you shine a light on the sensor, the number will go to the maximum of the sensor.

With that, the possibilities are immense to work with this sensor.

Next Lesson

Previous Lesson

✨ Our Gratitude and Next Steps

We sincerely hope this guide has been useful and enriching for your projects! Thank you for dedicating your time to this content.

Your Feedback is Invaluable:

Have any questions, suggestions, or corrections? Feel free to share them in the comments below! Your contribution helps us refine this content for the entire ElCircuits community.

If you found this guide helpful, spread the knowledge!

🔗 Share This Guide

Best regards,

The ElCircuits Team ⚡

O post Arduino Lesson 7 – Controlling LED via Serial Monitor apareceu primeiro em Electronic Circuits.

]]>
https://www.elcircuits.com/arduino-lesson-7-led-control-serial-monitor/feed/ 0
Arduino Lesson 6 – How to Use Analog Output to Fade an LED https://www.elcircuits.com/arduino-lesson-6-analog-output-led-fade/ https://www.elcircuits.com/arduino-lesson-6-analog-output-led-fade/#respond Fri, 01 Oct 2021 22:19:00 +0000 https://elcircuits.com/arduino-lesson-6-how-to-use-analog-output-to-fade-an-led/ Arduino: Lesson 6 – How to use analog output to fade an LED Welcome to Lesson 6 – Basic Arduino Course In today’s lesson, we will learn how to use an analog output to increase and decrease the brightness of a LED. In this example, we will use the analogWrite() function, which triggers a PWM wave on an Arduino pin. We will use PWM to vary the brightness of a LED as an example, but nothing prevents us from also using it to drive a motor and control its speed, the concept is the same. The analogWrite() function triggers a square wave at the specified duty cycle until the next analogWrite() call. The frequency of the PWM signal is around 490 Hz on most Arduinos, however on Arduino Uno and some similar boards, pins 5 and 6 use a frequency of about 980 Hz. On most Arduino boards with ATmega168 or ATmega328 chips, this function works on pins 3, 5, 6, 9, 10 and 11. On the Arduino Mega this function works on pins 2 to 13 and 44 to 46. Hardware Required Arduino Board LED 200 ohms resistor Jumper Wires Protoboard (optional) The Circuit The circuit is quite simple, we connect a LED in series with a 220 ohm resistor used to limit the current in the LED as we learned in the previous lessons, and we connect the PWM 9 port of the Arduino UNO as shown in Figure 2 below. Fig. 2 – Using an analog output to fade an LED – tinkercad.com We use a Protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino. The Code The analogWrite() function writes an analog value to the Arduino pin. It is important to remember that this output is not a pure analog output that we can use to generate a pure sine wave, but this output is a PWM wave control. When we execute the call to the analogWrite() function, the pin will generate a constant square wave with the specified duty cycle until the next function call. After building the circuit, connect your Arduino board to your computer, launch Arduino Software (IDE), copy the code below and paste it into your Arduino IDE. But first let us understand the code line by line. In Line 3, we declared ledPin to digital Pin 9 where we connect the LED to the digital Pin 9. In Line 5, we enter the void setup() function. This function is read only once when the Arduino is started. In Line 6, we define Port 9 as the output, using the pinMode(); function; 010203040506070809 //Arduino: Lesson 6 – How to use Analog Output to fade an LED int ledPin = 9;                                // LED connected to digital pin 9 void setup() {                                    // This function is called once when the program startspinMode(ledPin, OUTPUT);        // Set the digital pin as output: }//———————- www.elcircuits.com —————————– In Line 09, we enter in the loop() function does precisely what its name suggests, and loops consecutively. In Line 11, the control structure For, is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. In Line 12, we run the analogWrite() function, this function writes an analog value (PWM wave) to pin 9, in this case we are increasing the PWM value from 5 to 5, to increase the LED brightness up to the maximum value of 255. In Line 13, we use the function delay(); to wait for 30 milliseconds to see the dimming effect. 1…0910111213141516 // Arduino: Lesson 6 – How to use Analog Output to fade an LED void loop() { // The loop function runs over and over again forever  for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5 { // fade in from min to max in increments of 5 points:   analogWrite(ledPin, fadeValue);          // sets the value (range from 0 to 255)   delay(30);                                             // wait for 30 milliseconds to see the dimming effect  }//————————– www.elcircuits.com ———————————- In Line 16, the control structure For, is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop. In Line 17, we run the analogWrite() function, this function writes an analog value (PWM wave) to pin 9, in this case, we are decreasing the PWM value from 5 to 5, to decrease the LED brightness to the minimum value of 0. In Line 18, we use the function delay(); to wait for 30 milliseconds to see the dimming effect. 1…16171819202122 // Arduino: Lesson 6 – How to use Analog Output to fade an LED for (int fadeValue = 255; fadeValue >= 0; fadeValue -=5){ //fade out from max to min in increments of 5 points   analogWrite(ledPin, fadeValue);           // sets the value (range from 255 to 0)   delay(30);                                              // wait for 30 milliseconds to see the dimming effect  }}//———————– www.elcircuits.com ——————————  Below you can see the full code, which we can be copying and pasting into your Arduino IDE, and uploading to Arduino. The complete code is showed in the sketch below! 1234567891011121314151617181920212223 // Arduino: Lesson 6 – How to use Analog Output to fade an LED int ledPin = 9;                                          // LED connected to digital pin 9 void setup() {                                           // This function is called once when program starts  pinMode(ledPin, OUTPUT);                   // Set the digital pin as output: }void loop() { // The loop function runs over

O post Arduino Lesson 6 – How to Use Analog Output to Fade an LED apareceu primeiro em Electronic Circuits.

]]>

Arduino: Lesson 6 - How to use analog output to fade an LED

Arduino: Lesson 6 – How to use analog output to fade an LED

Welcome to Lesson 6 – Basic Arduino Course

In today’s lesson, we will learn how to use an analog output to increase and decrease the brightness of a LED. In this example, we will use the analogWrite() function, which triggers a PWM wave on an Arduino pin.

We will use PWM to vary the brightness of a LED as an example, but nothing prevents us from also using it to drive a motor and control its speed, the concept is the same.

The analogWrite() function triggers a square wave at the specified duty cycle until the next analogWrite() call.

The frequency of the PWM signal is around 490 Hz on most Arduinos, however on Arduino Uno and some similar boards, pins 5 and 6 use a frequency of about 980 Hz.

On most Arduino boards with ATmega168 or ATmega328 chips, this function works on pins 3, 5, 6, 9, 10 and 11. On the Arduino Mega this function works on pins 2 to 13 and 44 to 46.

Hardware Required

  • Arduino Board
  • LED
  • 200 ohms resistor
  • Jumper Wires
  • Protoboard (optional)

The Circuit

The circuit is quite simple, we connect a LED in series with a 220 ohm resistor used to limit the current in the LED as we learned in the previous lessons, and we connect the PWM 9 port of the Arduino UNO as shown in Figure 2 below.

Using an analog output to fade an LED - tinkercad.com

Fig. 2 – Using an analog output to fade an LED – tinkercad.com

We use a Protoboard to facilitate the connections, but you can also connect the wires directly to the Arduino.

The Code

The analogWrite() function writes an analog value to the Arduino pin. It is important to remember that this output is not a pure analog output that we can use to generate a pure sine wave, but this output is a PWM wave control.

When we execute the call to the analogWrite() function, the pin will generate a constant square wave with the specified duty cycle until the next function call.

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

  • In Line 3, we declared ledPin to digital Pin 9 where we connect the LED to the digital Pin 9.
  • In Line 5, we enter the void setup() function. This function is read only once when the Arduino is started.
  • In Line 6, we define Port 9 as the output, using the pinMode(); function;
01
02
03
04
05
06
07
08
09
//Arduino: Lesson 6 – How to use Analog Output to fade an LED
 
int ledPin = 9;                                // LED connected to digital pin 9
 
void setup() {                                    // This function is called once when the program starts
pinMode(ledPin, OUTPUT);        // Set the digital pin as output:
 
}
//———————- www.elcircuits.com —————————–
  • In Line 09, we enter in the loop() function does precisely what its name suggests, and loops consecutively.
  • In Line 11, the control structure For, is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop.
  • In Line 12, we run the analogWrite() function, this function writes an analog value (PWM wave) to pin 9, in this case we are increasing the PWM value from 5 to 5, to increase the LED brightness up to the maximum value of 255.
  • In Line 13, we use the function delay(); to wait for 30 milliseconds to see the dimming effect.
1

09
10
11
12
13
14
15
16
// Arduino: Lesson 6 – How to use Analog Output to fade an LED
 
void loop() { // The loop function runs over and over again forever
 
 for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5 { // fade in from min to max in increments of 5 points:
   analogWrite(ledPin, fadeValue);          // sets the value (range from 0 to 255)
   delay(30);                                             // wait for 30 milliseconds to see the dimming effect
  }
//————————– www.elcircuits.com ———————————-
  • In Line 16, the control structure For, is used to repeat a block of statements enclosed in curly braces. An increment counter is usually used to increment and terminate the loop.
  • In Line 17, we run the analogWrite() function, this function writes an analog value (PWM wave) to pin 9, in this case, we are decreasing the PWM value from 5 to 5, to decrease the LED brightness to the minimum value of 0.
  • In Line 18, we use the function delay(); to wait for 30 milliseconds to see the dimming effect.
1

16
17
18
19
20
21
22
// Arduino: Lesson 6 – How to use Analog Output to fade an LED
 
for (int fadeValue = 255; fadeValue >= 0; fadeValue -=5){ //fade out from max to min in increments of 5 points
   analogWrite(ledPin, fadeValue);           // sets the value (range from 255 to 0)
   delay(30);                                              // wait for 30 milliseconds to see the dimming effect
  }
}
//———————– www.elcircuits.com ——————————
 
Below you can see the full code, which we can be copying and pasting into your Arduino IDE, and uploading to Arduino.

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
// Arduino: Lesson 6 – How to use Analog Output to fade an LED
 
int ledPin = 9;                                          // LED connected to digital pin 9
 
void setup() {                                           // This function is called once when program starts
  pinMode(ledPin, OUTPUT);                   // Set the digital pin as output:
 
}
void loop() { // The loop function runs over and over again forever
 
 for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {  // fade in from min to max in increments of 5 points:
   analogWrite(ledPin, fadeValue);           // sets the value (range from 0 to 255)
   delay(30);                                              // wait for 30 milliseconds to see the dimming effect
  }
 
   for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {  // fade out from max to min in increments of 5 points:
   analogWrite(ledPin, fadeValue);           // sets the value (range from 0 to 255)
   delay(30);                                              // wait for 30 milliseconds to see the dimming effect
  }
}
//—————— www.elcircuits.com ———————–

Next Lesson

Previous Lesson

✨ Our Gratitude and Next Steps

We sincerely hope this guide has been useful and enriching for your
projects! Thank you for dedicating your time to this content.

Your Feedback is Invaluable:

Have any questions, suggestions, or corrections? Feel free to share them
in the comments below! Your contribution helps us refine this
content for the entire ElCircuits community.

If you found this guide helpful, spread the knowledge!


🔗 Share This
Guide

Best regards,

The ElCircuits Team ⚡

O post Arduino Lesson 6 – How to Use Analog Output to Fade an LED apareceu primeiro em Electronic Circuits.

]]>
https://www.elcircuits.com/arduino-lesson-6-analog-output-led-fade/feed/ 0
Arduino Lesson 5 – Reading Potentiometer Values on Serial Monitor https://www.elcircuits.com/arduino-lesson-5-potentiometer-serial-monitor/ https://www.elcircuits.com/arduino-lesson-5-potentiometer-serial-monitor/#respond Sun, 19 Sep 2021 22:12:00 +0000 https://elcircuits.com/arduino-lesson-5-reading-potentiometer-and-showing-values-on-serial-monitor/ Arduino Lesson 5 – Reading Potentiometer Values on Serial Monitor Arduino Lesson 5 – Reading Potentiometer and Showing Values On Serial Monitor Welcome to Lesson 5 – Basic Arduino Course Today we are going to learn how to read a Potentiometer end showing the Values on Serial Monitor.In this example, we will use a potentiometer, however, this same concept is used for most analog sensors. What will differentiate will be the type of calculation used, with reference to each sensor. We will use the analogRead() function, to read A0 PIN, in Arduino we have 6 Analog Ports, which goes from A0 to A5, so we can make readings from up to 6 sensors simultaneously without having to use external hardware. Hardware Required Arduino Board A 10K Potentiometer Wires Protoboard (optional) The Circuit The potentiometer works like a voltage divider. When you turn the shaft of the potentiometer, you change the resistance on either side of the center pin (or wiper) of the potentiometer. This changes the relative resistances between the center pin and the two outer pins, so you get a different voltage at the analog input. When the shaft is fully rotated in one direction, there is no resistance between the center pin and the pin connected to ground. The middle pin varies from 0Kohms to 10Kohms, which is the value of the potentiometer we will use. This resistance is sensed by the voltages at the ends of the potentiometer, causing the center pin to vary between 0V and 5V. And the voltage variation that comes from the Center Pin of the potentiometer goes to the A0 Analog Port. In Figure 2, you can see the schematic of the potentiometer and the Arduino. We use a breadboard to make the connections easier, but you can also connect the wires directly from the A0 terminal in the middle of the potentiometer, connecting the outer pin to 5V positive and the outer pin to the negative GND of the Arduino. Fig. 2 – Reading Potentiometer and Showing Values On Serial Monitor – tinkercad.com The Code The analogRead() function converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit within the microcontroller called analog-to-digital converter, or ADC. Then, if the voltage at the center pin is 0 volts, analogRead() returns 0. When the shaft is turned all the way the other way, there is no resistance between the middle pin and the pin connected to 5 volts. If the voltage on the middle pin is then 5 volts, analogRead() returns 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the voltage applied to the pin. After building the circuit, connect your Arduino board to your computer, launch Arduino Software (IDE), copy the code below and paste it into your Arduino IDE. But first let us understand the code line by line. In Line 3, we declared variable sensorPin which is set to Analog Pin A0 where we receive the value of the connected potentiometer. In Line 4, we create a variable senorPin that stores the value of the sensor that we also use for the potentiometer. // Arduino: Lesson 5 - Reading Potentiometer and Showing Values On Serial Monitor int sensorPin = A0; // Read the Analogic Pin A0 Values into a variable int sensorValue = 0; // Variable to store the value coming from the sensor "Potentiometer" In Line 6, we enter the void setup() function. This function is read only once when the Arduino is started. In Line 7, we begin serial communication by declaring the Serial.begin() function. At 9600 bits of data per second, this is the speed at which your computer will communicate with your Arduino Serial. void setup() { // This function is called once when the program starts Serial.begin(9600); // Begin the Serial Monitor with bounce rate in 115200 } After setting the initializations, in Line 10 we will enter the void loop() function. In Line 11, we use the variable sensorValue to store the resistance value, which is between 0 and 10K (read by the Arduino as a value between 0 and 1023) and controlled by the potentiometer. In Line 13, we use the command Serial.println(), it is print out the value controlled by the potentiometer, this value will read with value between 0 and 1023 in your Serial Monitor. In Line 14, we run the delay() function, for greater system stability. void loop() { // The loop function runs over and over again forever sensorValue = analogRead(sensorPin); // Read the input on analog pin A0 Serial.println(sensorValue); // Print out the value you read delay(1); // delay in between reads for stability } Now you can copy the code and upload it to your Arduino. After that, if you open your Serial Monitor in Arduino Software (IDE) (by clicking on the icon that looks like a lens on the right side of the green top bar or using the keyboard shortcut Ctrl+Shift+M), you should see a steady stream of numbers in the range 0-1023 that correlate with the position of the pot. When you turn your potentiometer, these numbers respond almost instantly. The complete code is showed in the sketch below! // Arduino: Lesson 5 - Reading Potentiometer and Showing Values On Serial Monitor int sensorPin = A0; // Read the Analogic Pin A0 Values into a variable int sensorValue = 0; // Variable to store the value coming from the sensor "Potentiometer" void setup() { // This function is called once when the program starts Serial.begin(9600); // Start serial connection } void loop() { // The loop function runs over and over again forever sensorValue = analogRead(sensorPin); // Read the input on analog pin A0 Serial.println(sensorValue); // Print out the value you read delay(1); // delay in between reads for stability } //------------------------------------- www.elcircuits.com -------------------------------------------- Next Lesson Arduino: Lesson 6 – How to use Analog Output to fade an LED Arduino: Lesson 7 –

O post Arduino Lesson 5 – Reading Potentiometer Values on Serial Monitor apareceu primeiro em Electronic Circuits.

]]>
Fig. 1 - Arduino Lesson 5 - Reading Potentiometer and Showing Values On Serial Monitor

Arduino Lesson 5 – Reading Potentiometer and Showing Values On Serial Monitor


Welcome to Lesson 5 – Basic Arduino Course

Today we are going to learn how to read a Potentiometer end showing the Values on Serial Monitor.
In this example, we will use a potentiometer, however, this same concept is used for most analog sensors.

What will differentiate will be the type of calculation used, with reference to each sensor.

We will use the analogRead() function, to read A0 PIN, in Arduino we have 6 Analog Ports, which goes from A0 to A5, so we can make readings from up to 6 sensors simultaneously without having to use external hardware.

Hardware Required

  • Arduino Board
  • A 10K Potentiometer
  • Wires
  • Protoboard (optional)

The Circuit

The potentiometer works like a voltage divider. When you turn the shaft of the potentiometer, you change the resistance on either side of the center pin (or wiper) of the potentiometer.

This changes the relative resistances between the center pin and the two outer pins, so you get a different voltage at the analog input.

When the shaft is fully rotated in one direction, there is no resistance between the center pin and the pin connected to ground.

The middle pin varies from 0Kohms to 10Kohms, which is the value of the potentiometer we will use.

This resistance is sensed by the voltages at the ends of the potentiometer, causing the center pin to vary between 0V and 5V.

And the voltage variation that comes from the Center Pin of the potentiometer goes to the A0 Analog Port.

In Figure 2, you can see the schematic of the potentiometer and the Arduino. We use a breadboard to make the connections easier, but you can also connect the wires directly from the A0 terminal in the middle of the potentiometer, connecting the outer pin to 5V positive and the outer pin to the negative GND of the Arduino.

Fig. 2 - Reading Potentiometer and Showing Values On Serial Monitor - tinkercad.com

Fig. 2 – Reading Potentiometer and Showing Values On Serial Monitor – tinkercad.com

The Code

The analogRead() function converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit within the microcontroller called analog-to-digital converter, or ADC.

Then, if the voltage at the center pin is 0 volts, analogRead() returns 0. When the shaft is turned all the way the other way, there is no resistance between the middle pin and the pin connected to 5 volts.

If the voltage on the middle pin is then 5 volts, analogRead() returns 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the voltage applied to the pin.

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

  • In Line 3, we declared variable sensorPin which is set to Analog Pin A0 where we receive the value of the connected potentiometer.
  • In Line 4, we create a variable senorPin that stores the value of the sensor that we also use for the potentiometer.
// Arduino: Lesson 5 - Reading Potentiometer and Showing Values On Serial Monitor

int sensorPin = A0;    // Read the Analogic Pin A0 Values into a variable
int sensorValue = 0;   // Variable to store the value coming from the sensor "Potentiometer"
  • In Line 6, we enter the void setup() function. This function is read only once when the Arduino is started.
  • In Line 7, we begin serial communication by declaring the Serial.begin() function. At 9600 bits of data per second, this is the speed at which your computer will communicate with your Arduino Serial.
void setup() {                 // This function is called once when the program starts
  Serial.begin(9600);          // Begin the Serial Monitor with bounce rate in 115200
}
  • After setting the initializations, in Line 10 we will enter the void loop() function.
  • In Line 11, we use the variable sensorValue to store the resistance value, which is between 0 and 10K (read by the Arduino as a value between 0 and 1023) and controlled by the potentiometer.
  • In Line 13, we use the command Serial.println(), it is print out the value controlled by the potentiometer, this value will read with value between 0 and 1023 in your Serial Monitor.
  • In Line 14, we run the delay() function, for greater system stability.
void loop() {                                                // The loop function runs over and over again forever
  sensorValue = analogRead(sensorPin);     // Read the input on analog pin A0
  
  Serial.println(sensorValue);             // Print out the value you read
  delay(1);                                // delay in between reads for stability
}

Now you can copy the code and upload it to your Arduino. After that, if you open your Serial Monitor in Arduino Software (IDE) (by clicking on the icon that looks like a lens on the right side of the green top bar or using the keyboard shortcut Ctrl+Shift+M), you should see a steady stream of numbers in the range 0-1023 that correlate with the position of the pot. When you turn your potentiometer, these numbers respond almost instantly.

The complete code is showed in the sketch below!

// Arduino: Lesson 5 - Reading Potentiometer and Showing Values On Serial Monitor

int sensorPin = A0;    // Read the Analogic Pin A0 Values into a variable
int sensorValue = 0;   // Variable to store the value coming from the sensor "Potentiometer"

void setup() {                 // This function is called once when the program starts
  Serial.begin(9600);          // Start serial connection
}

void loop() {                                                // The loop function runs over and over again forever
  sensorValue = analogRead(sensorPin);     // Read the input on analog pin A0
  
  Serial.println(sensorValue);             // Print out the value you read
  delay(1);                                // delay in between reads for stability
}
//------------------------------------- www.elcircuits.com --------------------------------------------

Next Lesson

Previous Lesson

✨ Our Gratitude and Next Steps

We sincerely hope this guide has been useful and enriching for your projects! Thank you for dedicating your time to this content.

Your Feedback is Invaluable:

Have any questions, suggestions, or corrections? Feel free to share them in the comments below! Your contribution helps us refine this content for the entire ElCircuits community.

If you found this guide helpful, spread the knowledge!

🔗 Share This Guide

Best regards,
The ElCircuits Team ⚡

O post Arduino Lesson 5 – Reading Potentiometer Values on Serial Monitor apareceu primeiro em Electronic Circuits.

]]>
https://www.elcircuits.com/arduino-lesson-5-potentiometer-serial-monitor/feed/ 0
Arduino Lesson 4 – How to Read a Pushbutton using digitalRead() https://www.elcircuits.com/arduino-lesson-4-read-pushbutton-digitalread/ https://www.elcircuits.com/arduino-lesson-4-read-pushbutton-digitalread/#respond Sun, 05 Sep 2021 00:27:00 +0000 https://elcircuits.com/arduino-lesson-4-read-pushbutton-with-digitalread-function/ 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, Arduino will read and light an LED indicating that button was pressed. We will use digitalRead() function. Hardware Required Arduino Board A Pushbutton LED – optional “You can use onboard LED” 220 ohm resistor – optional “You can use 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 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 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 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. // 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 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 ledPin declaration as output. In Line 12 we declare our buttonPin as input Pullup, which means we are using Arduino’s internal Pullup resistor, so we don’t need to use an external resistor together with the switch. void setup() { pinMode(ledPin, OUTPUT); // Initialize digital pin 6 as an output. pinMode(buttonPin, INPUT_PULLUP); // Initialize pushbutton pin as an input pull up } After setting the initializations, in Line 15 we will enter the void loop() function. In Line 16, we get the state of the switch, when pressed, variable gets “0“, and when released, variable gets “1“. You may be thinking, but it shouldn’t be the other way around, when you press it goes to “1” and when I release it it goes to “0“. Well, remember we use Arduino’s internal Pullup resistor, which 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 ground, which means we ground output “0” so it goes to LOW. In Line 18, we declare a control structure, using IF conditional, with it we are going to check if the key was pressed, if so, variable buttonState will receive value LOW, “0“. If released, it will receive HIGH value “1“. In Line 19, digitalWrite function, command activates ledPin to HIGH level, which means that it goes from 0V to 5V, which makes the LED receive voltage and light up. In Line 20, we have “else” control structure, that will be triggered if the “if” structure control isn’t true. In Line 21, digitalWrite function, command activates ledPin to LOW level, which means that it goes from 5V to 0V, which makes the LED turn off. 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, buttonState is LOW digitalWrite(ledPin, HIGH); // Turn LED On } else { digitalWrite(ledPin, LOW); // Turn LED Off } } The complete code is shown in the sketch below! // 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() { pinMode(ledPin, OUTPUT); // Initialize digital pin 6 as an output. pinMode(buttonPin, INPUT_PULLUP); // Initialize 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 == LOW) { // Check if pushbutton is pressed. Case is, buttonState is LOW digitalWrite(ledPin, HIGH); // Turn LED On } else { digitalWrite(ledPin, LOW); // Turn LED Off } } //------------------------------------- www.elcircuits.com -------------------------------------------- Next Lesson Arduino: Lesson 5 – Reading Potentiometer and Showing Values On Serial Monitor Arduino: Lesson 6 – How to use Analog Output to fade an LED Arduino: Lesson 7 – Controlling LED Through Serial Monitor with Arduino Previous Lesson Arduino: Lesson 1 – What is Arduino? Arduino: Lesson 2 – How to Install Arduino Software (IDE) on Windows – Step by Step! Arduino: Lesson 3 – Blinking an LED with delay() function ✨ Our Gratitude and Next Steps We sincerely hope this guide has been useful and enriching for

O post Arduino Lesson 4 – How to Read a Pushbutton using digitalRead() apareceu primeiro em Electronic Circuits.

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

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, Arduino will read and light an LED indicating that button was pressed. We will use digitalRead() function.

Hardware Required

  • Arduino Board
  • A Pushbutton
  • LED – optional “You can use onboard LED”
  • 220 ohm resistor – optional “You can use 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 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 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

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 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.
// 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 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 ledPin declaration as output.
  • In Line 12 we declare our buttonPin as input Pullup, which means we are using Arduino’s internal Pullup resistor, so we don’t need to use an external resistor together with the switch.
void setup() {
  pinMode(ledPin, OUTPUT);              // Initialize digital pin 6 as an output.
  pinMode(buttonPin, INPUT_PULLUP);       // Initialize pushbutton pin as an input pull up
}
  • After setting the initializations, in Line 15 we will enter the void loop() function.
  • In Line 16, we get the state of the switch, when pressed, variable gets “0“, and when released, variable gets “1“.

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

Well, remember we use Arduino’s internal Pullup resistor, which 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 ground, which means we ground output0” so it goes to LOW.

  • In Line 18, we declare a control structure, using IF conditional, with it we are going to check if the key was pressed, if so, variable buttonState will receive value LOW, “0“. If released, it will receive HIGH value “1“.
  • In Line 19, digitalWrite function, command activates ledPin to HIGH level, which means that it goes from 0V to 5V, which makes the LED receive voltage and light up.
  • In Line 20, we have “elsecontrol structure, that will be triggered if the “if” structure control isn’t true.
  • In Line 21, digitalWrite function, command activates ledPin to LOW level, which means that it goes from 5V to 0V, which makes the LED turn off.
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, buttonState is LOW
    digitalWrite(ledPin, HIGH);         // Turn LED On
  } else {
    digitalWrite(ledPin, LOW);          // Turn LED Off
  }
}

The complete code is shown in the sketch below!

// 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() {
  pinMode(ledPin, OUTPUT);              // Initialize digital pin 6 as an output.
  pinMode(buttonPin, INPUT_PULLUP);       // Initialize 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 == LOW) {            // Check if pushbutton is pressed. Case is, buttonState is LOW
    digitalWrite(ledPin, HIGH);         // Turn LED On
  } else {
    digitalWrite(ledPin, LOW);          // Turn LED Off
  }
}
//------------------------------------- www.elcircuits.com --------------------------------------------

Next Lesson

Previous Lesson

✨ Our Gratitude and Next Steps

We sincerely hope this guide has been useful and enriching for your projects! Thank you for dedicating your time to this content.

Your Feedback is Invaluable:

Have any questions, suggestions, or corrections? Feel free to share them in the comments below! Your contribution helps us refine this content for the entire ElCircuits community.

If you found this guide helpful, spread the knowledge!

🔗 Share This Guide

Best regards,
The ElCircuits Team ⚡

O post Arduino Lesson 4 – How to Read a Pushbutton using digitalRead() apareceu primeiro em Electronic Circuits.

]]>
https://www.elcircuits.com/arduino-lesson-4-read-pushbutton-digitalread/feed/ 0