Posted on by & filed under Arduino, Electronics, Schools & Curriculum, Software and Apps.

Lessons Menu:

Lesson 3 Hardware:

  1. Computer / Laptop or Netbook
  2. Arduino Microcontroller
  3. USB to Serial Adapter (if your microcontroller does not have a USB port)
  4. Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)
  5. Potentiometer (rotary or linear) Example: DFRobot Rotation Sensor
  6. Optional secondary LED.

Open the sample sketch “AnalogInput” under File -> Examples -> Analog. The comments section has been reduced below to make the code clearly visible.

We see several new lines of code here:

int sensorPin = A0;

The “int” is short for “integer”. The name “sensorPin” was chosen only to describe what the variable represents; “sensor pin”. The fact that the ‘P’ is capitalized makes it easier to see that it is actually two words, since spaces cannot be used. The integer “sensor pin” is equal to A0, where “A0″ is Analog pin 0 on the Arduino. On its own, “A0″ is not a reserved term. However, when used in context, the system recognizes it as analog pin 0. The line must be ended with a semicolon. By declaring a variable in the setup, you can use the term, which in this case is “sensorPin”, throughout the code instead of “A0″. There are two main benefits to this:

1) It makes the code more descriptive

2) If you want to change the value of the variable, you only need to do it in one place.

sensorValue = analogRead(sensorPin);

This line uses the term “analogRead” in order to read the voltage of an analog pin. Most Arduino microcontrollers use 10 bit analog (voltage) to digital (numeric) conversion, which is 210 possible numbers = 1024. Therefore a voltage of 0V corresponds to a numeric value of 0. A voltage of 5V corresponds to a numeric value of 1024. Therefore a value of 3V would correspond to a numeric value of:

3/5 =x/1024, x = 3*1024/5 = ~614

Alternatively you could have written: sensorValue = analogRead(A0);

int ledPin = 13;

Once again, the term “ledPin” is not a reserved word in Arduino, it was chosen to describe which pin was connected to the LED. The value “13″ is a normal value, but just like “A0″, when used in context represents pin 13.

int sensorValue = 0;

The term “sensorValue” is not a reserved term either.

Connect the potentiometer to pins A0, 5V and GND. The middle (wiper) lead is the one to connect to the analog pin and the voltage varies on this pin. The orientation of the other two pins does not matter. The other option is to connect the potentiometer to pins A0, A1 and A2. However, you will need to add the following code under void setup():

digitalWrite (A1, LOW);

digitalWrite (A2, HIGH);

This sets the corresponding pins to 0V (GND) and 5V (PWR). Once the potentiometer is connected, upload this sketch to the board and change to the Serial monitor. As you rotate the knob (or slide the slider), the values should change between 0 to 1023. Correspondingly, the LED will blink with a faster or shorter delay.

You can now read values and use them within your code. The new function used here is “analogRead();” where the pin selected is pin #2. If you used analog pin #5, you would change the code to read:

int sensorpin = 5;

If the system does not work, check the syntax and ensure the code uploads correctly. Next, check the connections to the potentiometer ensuring that the middle lead goes to the correct pin, and the other pins are powered at 0V and 5V. If you bought a very cheap or old potentiometer, there is a chance it may be mechanically defective. You can test this using a multimeter and connect the ends to the middle pin and an outer pin. Set the multimeter to read resistance and rotate the knob; if the resistance changes slowly, the pot is working. If the resistance is erratic, you need a new potentiometer.

Now what if you want to see the value yourself? Take a look at the code above and write it in the Arduino interface as a new sketch. Some new code you will see is:

Serial.println(sensorValue);

This sends the value contained in the variable “sensorValue” serially via the USB plug and digital pin 1. Verify, then upload this sketch to your Arduino. Once it is done, press on the “magnifying glass” located towards the top right of the window. This is the “Serial monitor” and monitors communications being sent and received by the Arduino. Here you must verify that the Baud rate is also 9600, or else you will see garbage.

If you did not want the values to appear on a new line every time, you could write

Serial.print(sensorValue);

Tags:

Leave a Reply

  • (will not be published)