Full width home advertisement

Electronics Hub

Internet of Networks

Post Page Advertisement [Top]


Analog In, Serial Out


Previously, We have discussed about working on Arduino board for Blinking LED, but this example is more procedural based.

This example mainly shows how to read an analog input pin, and map the outcome to a variety from 0 to 255. After that, use that result to set the pulse width modulation (PWM) of an output pin to dim or brighten an LED and print the values on the serial monitor of the Arduino Software (IDE). 

Components Required
  • Arduino or Genuino Board
  • Potentiometer
  • Red LED
  • 220 ohm Resistor

Connect a single pin from your pot to 5V, placing the center pin to analog pin 0 and all the other left over pins to ground. After that, connect a 220 ohm current limiting resistor to the digital pin 9 along with an LED in series. The long, positive leg (the anode) of the LED should be connected to the output from the resistor, with the shorter, negative leg (the cathode) connected to ground. 
analog in serial out, analog in serial out arduino
Free picture downloaded from unsplash.com
Code
There are mainly two variables, sensorValue and outputValue, which allows you to setup with a setup() function and to begin serial communication. 
Next, in the main loop, sensorValue is assigned to store the raw analog value read from the potentiometer. Arduino has an analogRead range from 0 to 1023, and an analogWrite range only from 0 to 255, consequently the data from the potentiometer needs to be converted into a smaller range before using it to faint the LED light.
Just to switch this value, use a function named map():
outputValue = map(sensorValue, 0, 1023, 0, 255);
outputValue is assigned to equal the scaled value from the potentiometer. map() accepts five arguments: The value to be mapped, the low range and high values of the input data, and the low and high values for that data to be remapped to. In this matter, the data fetched from the sensor is mapped down from its original range of 0 to 1023 to 0 to 255.
The newly mapped sensor data is then output to the analogOutPin dimming or brightening the LED as the potentiometer is turned. Finally, both the unprocessed and processed sensor values are sent to the Arduino Software Environment (IDE) serial monitor window, in a fixed stream of data.
const int analogInPin = A0;
const int analogOutPin = 9;

int sensorValue = 0;
int outputValue = 0;

void setup() 

{

Serial.begin(9600);


}

void loop() 
{

  sensorValue = analogRead(analogInPin);

  
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  analogWrite(analogOutPin, outputValue);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);
  delay(2);
}



No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Sriparna Sarkar