untung99play.xyz: Arduino Analog Input Output
Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.
Berikut adalah artikel atau berita tentang Harian untung99play.xyz dengan judul untung99play.xyz: Arduino Analog Input Output yang telah tayang di untung99play.xyz terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di koresponden@untung99play.xyz, Terimakasih.
I often wonder who would win if Frankenstein’s Monster and Dracula got into a fight. My personal bet is on the monster – but I would be concerned with Dracula doing some funky vampire mind control trick.
Now this lesson doesn’t use mind control, but it does use Arduino analog input to control the output of a digital pin. Controlling a digital pin with an analog input can be extremely helpful because many sensors report analog data.
This lesson explores how to use analog input to adjust the intensity of an LED.
If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it.
You Will Need
Step-by-Step Instructions
- Place the potentiometer into the breadboard pictured in the circuit diagram.
- Run a jumper wire from the 5-volt pin of the Arduino to either one of the outside pins of the potentiometer.
- Run another jumper wire from one of the ground pins on the Arduino (labeled GND) to the other outside pin of the potentiometer.
- Run the final jumper wire from pin A0 on the Arduino to the middle pin of the potentiometer.
- Place one end of the 220-ohm resistor into pin 9.
Place the short leg of the LED into the ground pin next to pin 13. - Connect the long leg of the LED to the other end of the resistor with the alligator clip.
- Plug the Arduino into your computer.
- Open the Arduino IDE.
- Open the sketch for this section.
- Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished.
- Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished.
- Open the serial monitor window – sensor input and output data should be streaming along.
- As you adjust the knob of the potentiometer the brightness of the LED should vary.
The Arduino Code
/* Analog input, analog output, serial output Reads an analog input pin, maps the result to a range from 0 to 255 and uses the result to set the pulsewidth modulation (PWM) of an output pin. Also prints the results to the serial monitor. The circuit: * potentiometer connected to analog pin 0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground * LED connected from digital pin 9 to ground created 29 Dec. 2008 modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. */ // These constants won't change. They're used to give names // to the pins used: const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue); // print the results to the serial monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); }
Discuss the Sketch
This sketch begins by declaring and initializing two variables, one for the potentiometer input pin and one for the LED output pin.
The variables are qualified as constants which keeps them safe from being changed anywhere else in the program:
const int analogInPin = A0; // Analog input pin the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to
The next variables created are integers used for storing input and output data:
int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out)
The setup() of this function is simple, we initiate serial communications with the Serial.begin() function:
void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }
Now we proceed to the last block of code, the loop().
The loop() first reads the value at analog pin A0 (where the potentiometer is attached) and assigns this value to a variable named sensorValue:
// read the analog in value: sensorValue = analogRead(analogInPin);
Recall that analogRead() returns a value between 0 and 1023. In this example, we use the input value we just recorded as the output value for the analogWrite() function. But wait a minute! The analogWrite() function will only accept values in the range of 0 through 255…the value we record with analogRead() can be much larger.
One solution to this problem is to rescale the input values to within the range of the analogWrite() function. The map() function does this easily:
// map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255);
Recall that map() takes 5 arguments:
map(Variable_to_be_Mapped, Low_Range, High_Range, New_Low, New_High)
In this case, we use the map() function to adjust the input value to correspond with the range of analogWrite(). Now that we have a value that will work with analogWrite(), let’s go ahead and get started with the output:
analogWrite(analogOutPin, outputValue);
As you may recall, analogWrite() takes two arguments 1) a pin number 2) the value to write. This function utilizes pulse width modulation, allowing you to adjust the power output of the PWM enabled pin (in this case pin 9 where we have the LED attached).
Recapping what we have done:
- Read the input from analog pin A0
- Adjusted the value to a condensed range using the map() function
- Passed the value to analogWrite() which in turn adjusts the brightness of the LED
Pretty simple – highly useful.
But what is a program without some type of data output? We get our fix by writing the input and output values to the serial port.
// print the results to the serial monitor: Serial.print(“sensor = ” ); Serial.print(sensorValue); Serial.print(“\t output = “); Serial.println(outputValue);
The \t in the third statement will insert a {TAB} into the string. Notice how the last Serial.println() function is different – it has “println” instead of just “print”. The println() function prints the outputValue variable and then starts a new line in the serial monitor window – this way all the information lines up nicely. A newline is like pressing the return key in a word editing program – it moves to the next line.
The last line of the sketch is the delay() function, which allows the analog-to-digital converter to stabilize before we feed it another input at the top of the loop().
Let’s review what this loop() does:
- Reads an input value at analog pin A0
- Assigns the value to a variable
- Changes the input value to match the scale of the output’s allowable range
- Assigns the newly mapped value to a variable
- Feeds the variable to analogWrite() to adjust the brightness of an LED
- Delays the program for 2 milliseconds to ensure the next reading is stable
Try On Your Own
- Instead of having the potentiometer adjust the brightness of the LED at pin 9, can you make it adjust the rate at which it blinks?
- Can you add another LED at pin 11, and have that LED get bright while the LED at pin 9 goes dark as you adjust the potentiometer?
Further Reading