May 15, 2021 Arduino
This example shows how to read the analog input at analog pin 0, convert the values in analogRead() into voltage, and output them to the serial monitor of the Arduino software (IDE).
You will need the following components:
Connect the components on the breadboard according to the circuit diagram, as shown in the following image.
Turn on the Arduino IDE software on your computer. U se arduino to encode and control your circuitry. /b11> Open a new sketch file by clicking New.
These 10 bar LED has many uses. C ompact footprint, simple connections, they are easy to use for prototypes or finished products. E ssentially, they are 10 separate blue LEDs, each with a separate anode and cathode connection.
They are also yellow, red and green.
Note - The pins on these bars may be different from those listed in the data sheet. /b10> Rotating the device 180 degrees corrects the change, making pin 11 the first pin.
/* LED bar graph Turns on a series of LEDs based on the value of an analog sensor. This is a simple way to make a bar graph display. Though this graph uses 8LEDs, you can use any number by changing the LED count and the pins in the array. This method can be used to control any series of digital outputs that depends on an analog input. */ // these constants won't change: const int analogPin = A0; // the pin that the potentiometer is attached to const int ledCount = 8; // the number of LEDs in the bar graph int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // an array of pin numbers to which LEDs are attached void setup() { // loop over the pin array and set them all to output: for (int thisLed = 0; thisLed < ledCount; thisLed++) { pinMode(ledPins[thisLed], OUTPUT); } } void loop() { // read the potentiometer: int sensorReading = analogRead(analogPin); // map the result to a range from 0 to the number of LEDs: int ledLevel = map(sensorReading, 0, 1023, 0, ledCount); // loop over the LED array: for (int thisLed = 0; thisLed < ledCount; thisLed++) { // if the array element's index is less than ledLevel, // turn the pin for this element on: if (thisLed < ledLevel) { digitalWrite(ledPins[thisLed], HIGH); }else { // turn off all pins higher than the ledLevel: digitalWrite(ledPins[thisLed], LOW); } } }
The sketch works like this: First, you read the input. /b10> Map the input value to the output range, in this case ten LEDs. /b11> Then, you set up a for-loop to iterate the output. /b12> If the number of outputs in the series falls below the mapped input range, open it. /b13> If not, turn it off.
As the value of the analog reading increases, you will see the LEDs turn on one by one, and when the readings decrease, the LEDs turn off one by one.