Home > Projects > Analog In, Out Serial using Arduino

Analog In, Out Serial using Arduino

Summary of Analog In, Out Serial using Arduino


This article explains how to control LED brightness using an Arduino. It reads analog input from a potentiometer, maps the 0-1023 range to 0-255, and uses PWM on digital pin 9 to adjust the LED intensity.

Parts used in Analog In, Out Serial using Arduino:

  • Arduino Board
  • Potentiometer
  • LED
  • 220 ohm resistor

This example shows how to read an analog input pin, map the result to a range from 0 to 255, and then use that result to set the pulsewidth modulation (PWM) of an output pin to dim or brighten an LED.

Circuit

Arduino Analog In, Out Serial

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Connect one pin from your pot to 5V, the center pin to analog pin 0, and the remaining pin to ground. Next, connect a 220 ohm current limiting resistor to digital pin 9, 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.

Schematic

Arduino Analog In, Out Serial schematic

Code

In the program below, after declaring two pin assignments (analog 0 for your potentiometer and digital 9 for your LED) and two variables, sensorValue and outputValue, the only thing that you do will in the setup function is to begin serial communication.

Next, in the main loop of the code, sensorValue is assigned to store the raw analog value coming in from the potentiometer. Because the Arduino has an analogRead resolution of 0-1023, and an analogWrite resolution of only 0-255, this raw data from the potentiometer needs to be scaled before using it to dim the LED.

In order to scale this value, use a function called map()

outputValue = map(sensorValue, 0, 1023, 0, 255);
Major Components in Project

Hardware Required

  • Arduino Board
  • Potentiometer
  • LED
  • 220 ohm resistor

For more detail: Analog In, Out Serial using Arduino

Quick Solutions to Questions related to Analog In, Out Serial using Arduino:

  • How do you connect the potentiometer?
    Connect one pin to 5V, the center pin to analog pin 0, and the remaining pin to ground.
  • What is the purpose of the 220 ohm resistor?
    It acts as a current limiting resistor connected in series with the LED.
  • Which pins are assigned for the project?
    Analog pin 0 is used for the potentiometer and digital pin 9 is used for the LED.
  • Why is the map function necessary?
    The Arduino has an analogRead resolution of 0-1023 but an analogWrite resolution of only 0-255, requiring scaling.
  • How does the code scale the sensor value?
    It uses the map function to convert values from the 0-1023 range to the 0-255 range.
  • What happens in the setup function?
    The setup function initializes serial communication.
  • Where is the raw analog value stored?
    The raw analog value from the potentiometer is stored in the variable sensorValue.
  • How is the LED brightness controlled?
    The mapped output value sets the pulsewidth modulation (PWM) of digital pin 9 to dim or brighten the LED.
  • Which leg of the LED connects to the resistor?
    The long positive leg (anode) connects to the output from the resistor.
  • Where is the negative leg of the LED connected?
    The shorter negative leg (cathode) is connected to ground.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top