Книга: Arduino: Master The Arduino Basics
Назад: Working on Your First Sketch
Дальше: Chapter 5: How to Store Data on Arduino

The Arduino board is a little bit different from some of the other projects that you have worked on. Instead of being able to look at a computer screen to get the information that you want like with a lot of traditional programming, you are sending things over to a board and getting it to behave. This is how things have to work if you want to get the programming over to a two or another electronic project. But since we are working with the board, there will be a few different things that we have to learn how to do to make it happen. 

In this chapter, we are going to take a look at some of the analog features that come with the Arduino board. We will look at how to read the input of the analog when it is on pin0, how to convert the values from your analog read() and change it over to voltage, and then get the serial monitor to print off in our IDE. 

First, you have to make sure that you have the right hardware in place to make this project work. This one will be pretty simple. You need to make sure that you have an Arduino board set up and ready to go. You will also need to have a 10k ohm potentiometer as well.

The second thing that we need to work on is getting all the wires hooked up to the right place. We need to take the three wires that come from our potentiometer and get them hooked up to the board. The first of the wires will go to the ground, but it should start with one of the outer pins on your potentiometer. Then you can take the second wire and connect it to the 5 volts on the Arduino board and then to the other outer pin on your potentiometer. Then the third one will start on the middle pin of your potentiometer, and it will be placed in the analog input 2.

Now that all of the wires are in place, it is time to turn the shaft of the potentiometer. When you do this, you will change the amount of resistance that is coming through on either side of the wiper, or the part that is connected to your center pin. This will basically change how much voltage is on the center pin. When you change the voltage so that the resistance between the side that is connected to the 5 volts and the center gets closer to zero, the voltage at the center is going to get close to five volts. When the resistances are reversed, the voltage for your center pin will change to 0, or to the ground. This input that you are reading is known as the analog voltage.

When you work on the Arduino board, you will notice that there is a circuit found inside of the microcontroller that is known as the analog-to-digital converter. We are going to call it the ADC. This is going to be in charge of reading the changing voltage and then will convert it to a number that is somewhere between 0 and 1023. If you turn your shaft to one side all the way, there will be 0 volts that will reach the pin, so the input value that goes there is going to be 0. But then if you turn the shaft to the other directions, this means that the pin is going to start receiving 5 volts, so your input value is going to be 1023. Of course, there will be times when the voltage will be somewhere in between 0 and 5, so the analogRead() is going to give you a number that falls between the 0 and 1023 based on how much voltage has been applied to the pin.

It is now time to look at the code that we will use to make all of this happen. In the program that we will write below, the first thing that we do will be to use the setup function to begin our serial communication. We will make sure that we have it set up to be 9600 bits of data each second and this will occur between the computer and the board.

Once that is done, it is time to work on the main loop for this code. Inside that main loop, we need to work on establishing a variable that will store the resistance value (remember that this is in between 0 and 1023), that is coming from the potentiometer. You can also go through and change the values from 0 to 1023 to a range that will correspond with the voltage your pins are reading. If you do this, you must make sure to create another variable, which is known as a float, and then add in a bit of math. To scale these numbers between 0 and 5, you need to divide 5 by 1023 and then multiply that by our sensorValue.

And finally, we need to make sure that we tell the system that we want to print out the information so that it shows up in our serial window in the end. This is pretty simple to do. Just add the Serial.println() to the last line of the code, and you are all set to go.

Once the code has all be completed, you should be able to open up the IDE and then open your Serial Monitor. If you are having trouble finding the Serial Monitor, just click on the icon that is to the right of your top green bar. You can also click on Control, Shift, and M to make this happen. Once the Serial Monitor is all open, you should see a nice steady stream of numbers that range somewhere between 0 and 5. As you turn your little switch from one direction to another, the values are going to change, and you will see that the voltage that corresponds to that change will come up on pin A0.

So, when you are ready to write out the code that you would like to use for this serial reading, you will use the following:


// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:



// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin 0:

int sensorValue = analogRead(A0);

// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):

float voltage = sensorValue * (5.0 / 1023.0);

// print out the value you read:



And that is all that you need to do in order to set up your analog pins and get them to work for you. There are other things that you can do with these analog pins, but these basics will help you to learn how they work and how to get it set up so that the IDE is able to read them properly.

Назад: Working on Your First Sketch
Дальше: Chapter 5: How to Store Data on Arduino