Analog inputs with Arduino UNO board.[Code and circuit diagram]

Hello guys, Welcome back to my blog. I explained in the last article how to get analog outputs. In previous articles, I have explained how to get the output from the Arduino board. Today we are going to show you how to get input from the Arduino board. We can do it easily. I have used analog pins from the Arduino board for this project. The Arduino UNO and Arduino NANO boards have six analog pins. These pins are numbered from A0 to A5. One analog PIN gives a value from 0 to 1024. This is called an analog read. We can get an analog reading by connecting the sensors and external parts to the Arduino board. Okay, let’s do it practically. I have used a potentiometer (variable resistor) for this project and I’ve used the serial monitor for watching reading.

So let’s see doing this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect the potentiometer to the breadboard.

Step 3

Thirdly, connect this circuit to the Arduino board. I have used the A0 analog pin for this project. This pin should be attached to the center of the potentiometer. The other pins on the potentiometer must be attached to the VCC and GND pins.

Step 4

Now, copy and paste the following program to the Arduino IDE.

void setup() {
  Serial.begin(9600);//enable serial monitor
}
void loop() {
  int value = analogRead(A0);//Read the analog value and put it in the variable
  Serial.println(value);//Print value
}

The serial monitor is enabled by the code of the void setup function. The void loop code reads the analog value and puts it into the variable.

int value – This code creates an integer variable. Its name is value. The value of the analog readings in this variable is stored.

analogRead(A0); – This code reads the analog value of the sensor or components connected to the A0 pin. This value is then added to the variable.

serial.println(value); – This code prints the value of the variable in the serial monitor.

Step 5

Now, select the board and port. After, click the upload button.

Now, rotate the potentiometer and see how the values ​​change. It is displayed on the serial monitor.

OK, let’s see how to use the Arduino Nano board with this project. The knowledge from the previous article has also been used in this project. Okay, let’s see how to do it step by step. For that, I used a potentiometer and an LED bulb. This project is done so that the LED brightness goes up and down when the potentiometer is turned.

Step 1

Firstly, assemble the following circuit on the breadboard.

Step 2

Secondly, copy and paste the following program to the Arduino IDE.

  • The complete program of this project – Download
void setup() {
  pinMode(9, OUTPUT);//define pin(PWM Pin)
  Serial.begin(9600);//serial monitor enable
}
void loop() {
  int brightness;//naming the veriable
  brightness = analogRead(A0);//Put the analog read value into the veriable
  brightness = map(brightness, 0, 1023, 0 , 255);//0-1023 to 0-255
  analogWrite(9, brightness);//led on and off
  Serial.println(brightness);//print serial monitor value
}

Step 3

Thirdly, select the board and port. After, click the upload button.

OK, enjoy this project. The full video guide is below. So, we hope to see you in the next project or tutorial. Have a good day.

Analog inputs with Arduino UNO board

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *