Lesson 98-9: LED Voltage Level Meter Using Arduino
In this lesson, we learn how to use an LED and a push button in this 10-LED project. The wiring diagram is shown; the code is fully explained and demonstrated with multiple changes of settings for each project. We read the potentiometer's voltage and display the voltage by turning each LED on to represent the voltage. A simple traffic light project.
This code is part of a video lecture. This is project 9's code. This code will turn on and off 5 LEDs such that only one will be turned on using a variable resistor or potentiometer. When the knob is rotated, it looks like the light is walking. You can add more LEDs to make it look fancier.
/*
* Arduino Step by Step Course by Robojax
* Lesson 98 https://robojax.com/course1/?vid=lecture98
* Project 9 of 10
*
* LED Voltage Level Meter
*
* This code will turn ON and OFF 5 LEDs such that only one will be turned ON using a variable resistor or potentiometer. When the knob is rotated it looks like light is walking. You can add more LEDs to make it look fancy.
* Watch full details and demonstration: https://youtu.be/OSwleCBlkuI
*
* Start a full Arduino Step By Step course: https://youtu.be/-6qSrDUA5a8
*
****** 10 Projects explained in this video
* 1-LED ON OFF
* 2-LED ON/OFF with push button
* 3-LED ON/OFF toggle with push button
* 4-Fading light of LED
* 5-Fading light of LED using push button
* 6-See Saw with LED
* 7-Walking Light
* 8-Walking light with push button
* 9-LED voltage level meter
* 10- Arduino Traffic Light
*
* LED = Light Emitting Diode
* LED is pronounced (El EE Dee)
* LED Voltage Bar
* This is an Arduino program that turns ON up to 5 LEDs according to
* the voltage read by the Arduino from a variable resistor (potentiometer) connected to
* pin A0
*
* written by Ahmad Shamshiri on Jan 31, 2011 at 22:17 in Ajax, Ontario, Canada
* This code can be obtained from Robojax.com
* Code ID: WalkingLED
This video is part of the Arduino Step by Step Course which starts here: https://youtu.be/-6qSrDUA5a8
* written by Ahmad Shamshiri on Jan 30, 2022
* in Ajax, Ontario, Canada
* www.Robojax.com
If you found this tutorial helpful, please support me so I can continue creating
content like this. Make a donation using PayPal or a credit card: https://bit.ly/donate-robojax
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*
*/
const int potPin = A0;//define a pin to connect potentiometer
const int LED1 = 7;
const int LED2 = 6;
const int LED3 = 5;
const int LED4 = 4;
const int LED5 = 3;
int LED_ON = LED1;
boolean showVoltage = true;//true or false to read voltage in serial monitor
float boardVoltage= 5.0;//for UNO and MEGA 5.0V for Nano either 5V or 3.3V
void setup() {
Serial.begin(9600);
Serial.println("LED Voltage Meter");
Serial.print("Board voltage ");
Serial.print(boardVoltage);
Serial.print("V");
pinMode(LED1, OUTPUT);//set LED1 pin as output
pinMode(LED2, OUTPUT);//set LED2 pin as output
pinMode(LED3, OUTPUT);//set LED3 pin as output
pinMode(LED4, OUTPUT);//set LED4 pin as output
pinMode(LED5, OUTPUT);//set LED5 pin as output
}//setup end
void loop() {
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (boardVoltage / 1023.0);
// // Watch full details and demonstraiton: https://youtu.be/7xTHaFdCyaI
if(showVoltage == true)
{
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.println("V");
}
delay(50);
// print out the value you read:
if (voltage >= boardVoltage*0.95)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
}else if (voltage >= boardVoltage * 0.80 )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, LOW);
}else if (voltage >= boardVoltage * 0.60 )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
}else if (voltage >= boardVoltage * 0.40 )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
}else if ( voltage > boardVoltage * 0.20 )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
}else
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
}
// Watch full details and demonstration: https://youtu.be/7xTHaFdCyaI
}//loop end
资源与参考
尚无可用资源。
文件📁
没有可用的文件。