AIR: Reading and Writing, Analog and Digital

Table of Contents

1 Overview

The goal of this lab is to learn about how some of the sensors and actuators for our Arduinos work. Some terminology: sensors read inputs, i.e., they collect information that the Arduino can work with. Actuators act, i.e. they do something that affects the world around them. A light detector is an example of a sensor; a speaker emitting a tone is an example of an actuator.

2 Communicate with your computer

One mundane but incredibly useful skill to have is to be able to have your Arduino communicate with your computer, and have your computer show you what the Arduino told it. Once we've got this capability, we can use it to understand and test a variety of capabilities. To get started…

  1. Plug in your Arduino to the power supply, connect it to your computer's USB port, start up the Arduino programming environment, and make sure you've got a blank sketch open.
  2. Then copy and paste this program:

    void setup()
    {
     Serial.begin(9600);
    }
    
    void loop()
    {
      Serial.println("Hello, world");
    }
    
    
  3. Look at the above code, and speculate as to what it all means. We'll eventually demystify all of it.
  4. Transfer the program to the Arduino. If you're having trouble, check the serial port setting in Tools; if nothing reveals itself there, ask for help.
  5. In the Arduino IDE, go to the Tools menu, then select Serial Monitor. Check that the baud rate in the bottom right corner of the Serial Monitor matches the rate specified in the program (in this case, 9600). If all goes well, you should see your message printing over and over again. Your Arduino is doing the work here: it is generating that text, and sending it back to your computer to see. Again, if you don't see this, ask for help.
  6. Modify the program so it prints out something else, such as your name.
  7. Modify the program so that it prints increasing integers, starting from 0. Hint: use a global variable to keep track of the count: there's an example of that in the previous lab.

3 Read from an analog sensor

One of the sensors that we have is a potentiometer, which is more informally referred to as a "pot." Find one; it's a brick with a small black knob on it. We can talk later about the electronics involved, but for purposes of this activity, you can think of it as a device where the position of the knob corresponds to a number. As you turn the knob, that corresponds to a different number. We'll use the Serial Monitor to see the value that the pot is producing.

  1. Find a pot and a "3s to 3s" cable. These are three wire cables, consisting of red, white, and black wires, that have 3-prong connectors at each end. There's another set of cables where one end consists of not a single 3-prong plug, but rather three separate plugs, one for each wire. Don't use that one (yet).
  2. Plug in one end of the connector into the pot. The pot actually has two different places to plug in; there's a three pin area labeled “G V S”, and another four pin area labeled “G V N S.” We'll be using the 3-pin connection. Orient the connector so that the pin labeled S (“signal”) is connected to the white wire, and the pin labeled with a minus sign (“ground”) is connected to the black wire. Plug the other end of the connector into the three pins on the Arduino labelled as A1. Orient the connector so that the white wire is towards the center of the board, and the black wire is near the edge of the board. Ask for help if you're having trouble finding A1, or unsure how to orient the wire.
  3. Copy and paste the following code into a blank Arduino sketch. See if you can figure out what the code is doing.

    void setup()
    {
      Serial.begin(9600);
    }
    
    
    void loop()
    {
      int value = analogRead(A1);
      Serial.println(value);
    }
    
  4. Upload your code. See if you can get it successfully transferred to your Arduino and running.
  5. Open up the Serial Monitor, and twist the knob on the pot back and forth. If all is going well, you should be able to see different values. Ask for help if you're having trouble.

Once you've got the above working, try these steps:

  1. Move the wires to a different set of pins on the Arduino. You can do an analog read from any of the pins labeled A0 through A15.
  2. Disconnect the pot, and go through the same exercise again with a light sensor. The light sensor has four pins on it, but for this exercise we're only going to use three of them, specifically those labeled VCC (voltage supply), GND (ground), and A0 (analog signal). To connect this device to the Arduino, you'll need one of the “flat to separate” cables that has a 3-pin connector on one end, and three separate plug on the other. First plug the 3-pin connector to the Arduino at one of the analog sets of pins, such as A5. Again, orient the connector so that the white wire is on the inside of the board, and the black wire is on the outside. Then plug the three separate leads on the other end of the cable into the light sensor, where the red wire connects to VCC, the black wire connects to GND, and the white wire connects to A0. Now run exactly the same program you've been running, making sure that you're reading from the correct pin number. If all goes well, you should be able to move the light sensor around, or cover it with your hand, and see the numbers change based on how much light it is seeing.
  3. Disconnect the light sensor, and try again with a sound sensor. This works nearly identically to the light sensor, except that it will display numbers that depend on the volume of sound it receives. If you don't see much happening, you may need to tweak its sensitivity. There's a tiny screw on top of a blue block that you can turn counter-clockwise to make the sensor more sensitive.
  4. Disconnect the light sensor, and try again with a joystick. The joystick is really just two pots; one that registers position in the x-direction, and one in the y-direction. So it's really just two sensors combined into one. Use the above setup again, but this time connect the voltage wire (red) to the joystick pin labeled +5V; connect the ground wire (black) to the joystick pin labeled GND, and the signal wire (white) to the pin labeled VRx. You should then see numbers changing around as you move the joystick back and forth in the x-direction. Move the white wire to the VRy pin, and you should then be able to see numbers changing in the y-direction.

4 Read from a digital sensor

A digital sensor is one that doesn't read a numeric value as such, but rather an "on/off," a "yes/no," or some other binary decision. One example of a digital sensor is a push button sensor.

  1. Find a push button sensor, and wire it up. The pins are labeled identically to the pot, so you should use the same color conventions.
  2. Connect the wires to the Arduino. This is just like connecting an analog input, except that this time you should use of the digital pins. These are labeled D0 through D63, though some of the numbers in the middle are missing. Oh, and D13 generally shouldn't be used for input. That's because it's hardwired to an LED light right on the Arduino itself; you may or may not remember from the last lab that we used D13 for blinking an LED on the Arduino itself. And don't use D0 or D1 if you're using Serial print in the same program, as they conflict with each other. Try pin D12. It's pretty easy to find. Make sure that you always have the signal wire on the inside of the Arduino, the ground wire on the outside, and the voltage wire in the middle.
  3. Now use the same program that you used for analog input, but instead of using analogRead, use digitalRead. Remember to set the pin number correctly. (Digital pins don't use a D in front of the digital pin numbers, but they match to the digital pins on the Arduino.) Do the above, upload, and open the Serial Monitor, If all goes well, you should be able to see an effect from pressing the push button.

5 Combine input and output

Combine the last lab and this one together. In other words, create a program that allows you to use an analog input (such as the pot or light sensor) to control the frequency of the sound coming out of the beeper. Your goal is to see if you can get your program set up so as you turn the knob on the pot or change the amount of light hitting the light sensor, the pitch coming out of the beeper changes. See how far you can get, and ask for help. If you complete this, see how many of our sensors and actuators you can get working.

6 Additional tasks

People will finish this lab at different speeds, which is fine. If you finish before the group as a whole is done, here are some more other ways you can try to extend the above:

  • Enable the user to use the push button sensor as an "on/off" toggle. You can't actually turn the Arduino off, or even stop the program from running, but you can have the program appear to be doing nothing when it is in the off mode. So for example, if you have succeeded in getting your program to make sounds that are controlled via the pot, the sounds should go off when the button is pressed, but come on again if the button is pressed again.
  • Rather than playing a fixed frequency based on the position of the pot, instruct the Arduino to play some sort of repeating sequence (perhaps something like a siren, or an ascending whoop). As the user turns the knob, that sequence increases in pitch in its entirety, but keeps repeating. You will likely need an additional loop in order to do this. You can visit the Arduino C++ documentation to see how to do for or while loops.
  • Instead of playing a sound at varying frequency based on the input, instead change the frequency that the pin 13 LED blinks at. So when the pot is at one extreme, the light blinks very slowly, and when the pot is at the other extreme, the light blinks very quickly.