Arduino Uno Example: Analog In Out Serial

This example shows how to use an analog input to control an analog output. The potentiometer in this circuit will be used to control the Pulse Width Modulation(PWM) of the LED. The sketch also uses the mapping function to take the analog input range (0 to 1023) and map it down to the analog output range (0 to 255).

Starting Example Code

//constants are used in this sketch to give names to the pins that do not change
const int analogInPin = A0;
const int analogOutPin = 11;

//Variable set up to store the value of the potentiometer.
int sensorValue = 0;

//Variable set up to store the output to the PWM (analog output)
int outputValue = 0;

void setup() {
	//Set up the serial communications
	Serial.begin(9600);
}

void loop() {
	//Read the value of the potentiometer
	sensorValue = analogRead(analogInPin);

	// map it to the range of the analog out
	outputValue = map(sensorValue, 0, 1023, 0, 255);

	//Set the PWM of the output LED to the mapped 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 looping back to the top
	delay(2);
}

Initial Code Wiring Diagram

Arduino-Uno-AnalogInOutSerial%20(1)

Expanded Code

For the expanded code, we will take and control 3 LED’s with PWM. To accomplish this, instead of mapping the Analog Input to 0 to 255, we will map it to 0 to 765, or 3 times the width. Then we will take the mapped value and use it to be able to tell what LED should currently be controlled by PWM. It will be similar to a volume meter or similar LED output showing how loud or how much power a device is using.

// Analog input pin that the potentiometer is attached to
const int analogInPin = A0;

// Analog output pin that the LED1 is attached to
const int analogOutPin1 = 9;

// Analog output pin that the LED2 is attached to
const int analogOutPin2 = 10;

// Analog output pin that the LED3 is attached to
const int analogOutPin3 = 11;

//Variable set up to store the value of the potentiometer.
int sensorValue = 0;

//Variable set up to store the output to the PWM (analog output)
int outputValue = 0;

void setup() {
	//Set up the serial communications
	Serial.begin(9600);
}

void loop() {
	//Read the value of the potentiometer
	sensorValue = analogRead(analogInPin);

	// map the output value to the range of the analog out(0 to 255) x 3
	outputValue = map(sensorValue, 0, 1023, 0, 765); 

	/*
	 * In the below set of code, we take the outputValue variable and divide
	 * it up to control 3 separate LEDs
	 */

	if (outputValue <= 255) {
		// if the potentiometer is in the first 3rd of travel

		// change LED 1 to the analog out value:
		analogWrite(analogOutPin1, outputValue);

		//make sure LED 2 and 3 are off
		digitalWrite(analogOutPin2, LOW);
		digitalWrite(analogOutPin3, LOW);

	} else if (outputValue >= 256 && outputValue <= 510) { 
		// if the potentiometer is in the middle 3rd of travel

		// Turn on LED 1
		digitalWrite(analogOutPin1, HIGH);

		// set LED 2 to the adjusted brightness.  Subtracting 255 gets us the correct value.
		analogWrite(analogOutPin2, outputValue - 255);

		// turn LED 3 off
		digitalWrite(analogOutPin3, LOW); 
	} else {
		// if the potentiometer is in the last 3rd of travel

		// Turn on LED 1
		digitalWrite(analogOutPin1, HIGH);

		// Turn on LED 2
		digitalWrite(analogOutPin2, HIGH);

		//We have to subtract the 510 to find the level of brightness for the 3rd LED to be set at
		analogWrite(analogOutPin3, outputValue - 510);
	}

	// print the results to the serial monitor
	Serial.print("sensor = ");
	Serial.print(sensorValue);
	Serial.print("\t output = ");
	Serial.println(outputValue);

	// wait 2 milliseconds before looping back to the top
	delay(2);
}

Expanded Code Wiring Diagram

AnalogInOutSerial-Advanced%20(1)

Bill of Materials

InOutSerial%20cart

Link to Cart: https://www.digikey.com/short/p8jc5m