Arduino Analog Read Potentiometer to Digital Out LED
10,796
The analog to digital sketches have been extensively covered using various components. To progress to more complex circuits and concepts, it is essential to understand these simpler ones. This tutorial will not delve as deeply as others due to the minimal amount of code involved. Here is the wiring diagram. This is a very simple circuit diagram. If there are inquiries regarding the software used for these diagrams, it is the widely adopted program for diagramming Arduino circuits called Fritzing, which is available for free and includes numerous well-known components, either built-in or downloadable from user submission pages. As illustrated, the analog signal is positive, and as the positive input increases, the digital output also increases. A pull-down resistor is connected directly to the potentiometer. An interesting note regarding the three terminals of the potentiometer is that swapping the positive and negative connections in the diagram will cause the LED to brighten when the potentiometer is turned counterclockwise (CCW) instead of clockwise (CW).
```c
/* Analog Potentiometer to Digital LED Sketch By: David M. Orlo */
byte potPin = 0; // Analog 0 connected to the potentiometer
byte LEDPin = 6; // Connected to LED on Pin 6
int potValue = 0; // Value returned from the potentiometer
void setup() {
pinMode(LEDPin, OUTPUT); // Set Pin 6 as an Output
}
void loop() {
potValue = analogRead(potPin) / 4; // Read the potentiometer, convert to 0 - 255
analogWrite(LEDPin, potValue); // Write the converted potentiometer value to LED pin
}
```
The simplicity of this circuit is evident. To modify the functionality, the LED can be programmed to pulse in relation to the potentiometer value while maintaining the same circuit layout. The code adjustments are as follows:
```c
/* Analog Potentiometer to Digital LED Sketch By: David M. Orlo */
byte potPin = 0; // Analog 0 connected to the potentiometer
byte LEDPin = 6; // Connected to LED on Pin 6
int potValue = 0; // Value returned from the potentiometer
void setup() {
pinMode(LEDPin, OUTPUT); // Set Pin 6 as an Output
}
void loop() {
potValue = analogRead(potPin); // Read the potentiometer
digitalWrite(LEDPin, HIGH); // Turn the LED on
delay(potValue); // Use the potentiometer value as a length of time to pause the microcontroller
digitalWrite(LEDPin, LOW); // Turn the LED off
delay(potValue); // Use the potentiometer value as a length of time to pause the microcontroller
}
```
The behavior observed is that the LED blinks faster when the potentiometer is turned to the left, which may seem counterintuitive. Adjustments can be made either in the code or through hardware modifications, as previously hinted regarding the potentiometer. It is also noted that a 1K resistor is utilized instead of the 10K resistor commonly found in other examples. In the scenario where the LED blinks faster as the photoresistor darkens, reversing the GND and POS connections and substituting the 1K resistor with a 10K resistor will achieve the desired effect of slower blinking.The analog to digital sketches have been covered a million ways from Sunday with every conceivable part but in order for us to move on to more complex circuits and concepts I need to be sure you know these simpler ones. This tutorial wont be quite as in depth as the others because frankly there just isn`t much code. Without further delay I give yo u the wiring diagram. Another very simple circuit diagram, by the way if you have been wondering what program I am using for these diagram and its actually the most widely used program fordiagrammingArduino circuits, its called Fritzing and you can get it here. Its completely Free and has tons of well known parts either built in directly or available for download on the user submission pages.
As shown above the Analog signal is positive and as the positive input increases the digital output increases as well. There is of course a pull down resistor in place which is directly connected to the potentiometer, an interested side note and the reason for the 3 legs on the potentiometer is that you can swap the positive and negative in the diagram above and the LED will get brighter when you turn the potentiometer to the left (CCW) instead of the right (CW).
/* Analog Potentiometer to Digital LED Sketch By: David M. Orlo */ byte potPin=0; //Analog 0 connected to the potentiometer byte LEDPin=6; //Connected to LED on Pin 6 int potValue=0; //Value returned from the potentiometer void setup(){ pinMode(LEDPin, OUTPUT); //Set Pin 6 as an Output } void loop(){ potValue = analogRead(potPin)/4; //Read the potentiometer, convert it to 0 - 255 analogWrite(LEDPin, potValue); //Write the converted potentiometer value to LED pin } So simple isn`t it Now lets change things up a bit and make the LED pulse in relation to the potentiometer value. We can keep the same circuit layout but we need to make a few tweaks to the code. /* Analog Potentiometer to Digital LED Sketch By: David M. Orlo */ byte potPin=0; //Analog 0 connected to the potentiometer byte LEDPin=6; //Connected to LED on Pin 6 int potValue=0; //Value returned from the potentiometer void setup(){ pinMode(LEDPin, OUTPUT); // Set Pin 6 as an Output } void loop(){ potValue = analogRead(potPin); //Read the potentiometer digitalWrite(LEDPin, HIGH); //Turn the LED on delay(potValue); //Use the potentiometer value as a length of time to pause the micro digitalWrite(LEDPin, LOW); //Turn the LED off delay(potValue); //Use the potentiometer value as a length of time to pause the micro } The first thing you will notice is that the LED blinks faster when you turn the pot to the left which seems opposite to whats intuitive, I will let you figure out how to reverse this either in code or in hardware using the hint I gave you above about the potentiometer.
The code is exactly the same so no changes needed there, You will notice I used a 1K resistor instead of a 10K thats used in other examples. In my example above you will notice the LED blinks faster as the photoresistor gets darker, lets say you want to reverse that and make it blink slower, simple just reverse the GND and POS and swap out the 1K resistor for a 10K.
Hysteresis in a lamp dimmer or other electrical appliances may cause issues when fine-tuning, leading to a feeling of being out of control. This is a circuit of.
Hysteresis in dimmer circuits can lead to undesirable fluctuations in brightness levels,...
The CD4069 or 74C04 hex inverter is utilized as a fixed-frequency oscillator centered around 100 kHz. U2 includes the variable frequency oscillator and balanced modulator. The CD4046 functions as a phase-locked loop, with R3, R4, and C2 determining the...
In Figure 1, the host microprocessor (uP) manages the HMC6352 through I2C serial data interface lines designated for data (SDA) and clock (SCL). Two external 10k-ohm pull-up resistors connected to a nominal 3-volt DC supply establish normally high logic...
The BQ2000 is a programmable, monolithic integrated circuit designed for fast-charge management of nickel cadmium (NiCd), nickel metal-hydride (NiMH), or lithium-ion (Li-Ion) batteries in single or multi-chemistry applications. The BQ2000 detects the battery chemistry and employs optimal charging and...
The circuit shown is a lighting control circuit that adjusts the speed of the flash output based on the strength of an audio signal. It utilizes eight flash integrated circuits (ICs) of the type LP188, which is housed in...
It has been determined that the Hijack method for obtaining serial data from the Mindflex to the iPhone will likely not be utilized. This decision is primarily based on the availability of most components needed to construct a similar...
We use cookies to enhance your experience, analyze traffic, and serve personalized ads.
By clicking "Accept", you agree to our use of cookies.
Learn more