Arduino Nano: Read the analog value of a potentiometer and display its corresponding digital value.

in #utopian-io7 years ago (edited)

Screenshot.jpg
photocredits

for this activity you must remember the concepts shown below.

Potenciometer

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value.

ADC

An Analog to Digital Converter (ADC) is a very useful feature that converts an analog voltage on a pin to a digital number. The arduino Nano has 8 analog inputs, each of them provides 10 bits of resolution. from A0 to A7.
the 10 bits of resolution means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023.

serial communication

Serial ports are the main way to communicate an Arduino board with a computer. Thanks to the serial port we can, for example, move the mouse

There are endless possibilities in which the use of the serial port is required. Therefore, the serial port is a fundamental component of a large number of Arduino projects, and it is one of the basic elements that we must learn in order to take full advantage of Arduino.

Serial ports

A port is the generic name with which we name interfaces, physical or virtual, that allow communication between two computers or devices.

A serial port sends the information through a sequence of bits. This requires at least two connectors to perform data communication, RX (reception) and TX (transmission). Sometimes you will see refer to the serial ports as UART.

Arduino and the Serial Port

Virtually all Arduino boards have at least one UART unit. Many models of Arduino boards have a USB or Micro USB connector connected to one of the serial ports, which simplifies the process of connecting to a computer. However some boards, such as the Mini Pro, do not have this connector so the only way to connect to them is directly through the corresponding pins.
we will connect using the USB cable.

Here we are going to see a photo of the arduino nano:

Screenshot002.jpg
PhotoCredits

Activity

we are going to read an external analog value given by the variable resistor of a potentiometer and we will see the corresponding digital value on the screen of the serial monitor tool of the Arduino IDE.

difficulty

This tutorial is for those who know the basics of Arduino.

Requirement

  • Arduino nano with USB Cable
  • Potenciometer
  • Laptop with Arduino IDE

Steps to follow

  • Prepare the equipment needed for the activity.
  • Connect the arduino to the Laptop using the usb cable
  • Open Arduino IDE

now we are going to open a new project in arduino:

Sin título.png

you will see this window:

Sin título1.png

Now we are going to connect the potentiometer to the arduino board as we will see in the following image:

foto1.jpg

in the following image we will see the code to read the analog voltage of pin A0, use the adc to obtain the digital value and print it on the serial monitor. Remember that you must have basic arduino notions.

Program

now you must write the following code that I have explained:

foto2.jpg

This is the text code:


int val_pot; //variable to store the analog value read
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); ////sets the baud rate of the UART for serial transmission
}

// put your main code here, to run repeatedly:
void loop() {

//reads the analog value of pin A0 connected to the potentiometer
val_pot = analogRead(A0);
//print out the following string
Serial.print("The digital value is: ");
//print out the value you read on the serial monitor
Serial.println(val_pot);
//Wait 10 ms for the next reading.
delay(100);
}


The serial port monitor is a small utility integrated within IDE Standard that allows us to easily send and receive information through the serial port. In the image we see how to open the serial monitor:

Sin título3.png

Using the serial monitor is very simple, and has two zones, one that shows the data received, and another to send them. You should check that the baud is 9600 as you configured it with the Serial.begin(9600) function.

Sin título4.png

After connecting the potentiometer to the analog input (pin A0) we will be able to vary the voltage in the center pin between 0 and 5V and display the digital values ​​on the serial monitor.

Sin título5.png

you should see values ​​from 0 to 1023 according to the position of the potentiometer knob.

try to run the code and start to learn.

Collaborations



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @luisrod I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]