In this lab and the lab next week, you will work together in a team, using one of the computers on the inside end of each row (center of the classroom). At the end of the lab session you will demonstrate your program to me, and all members of your team will get credit for completing the entire lab. So for this lab there is need to turn anything in to Sakai.
To run Blinky, first make sure your Arduino is plugged into your computer, using the USB cable I've provided. On the computers in our lab, you can plug the cable into one of the USB ports on the left side of the monitor. Once you do this, the LED will probably start blinking immediately, because Blinky comes installed as the default firmware sketch. Still, it will be worth going through the Blinky exercise.
As with IDLE3 for Python, Arduino has its own Integrated Development Environment (IDE). To launch the Arduino IDE, open a terminal window from your desktop (as you do to launch IDLE3), but instead of typing idle3, type arduino.
Once the Arduino IDE is up and running, the Arduino IDE should be ready to work with your Arduino Uno by default. To make sure that this is the case, go to the Tools menu. About halfway down the menu, you should see Board: Arduino/Genuino Uno. A few menu items below that, you should see Port: /dev/ttyACM0. If you don't see those values for the settings, use the menu to change the values. If you have difficulty doing that, let me know.
To upload (a.k.a flash) the Blinky sketch onto your Arduino, go to the File menu in the Arduino IDE, and follow the path to Examples / Basics / Blink. Another window will pop up, allowing you to work with the Blinky sketch. Since it's already pre-written for us, all we need to do is upload it onto our Arduino. You do this by clicking on the little → (right-arrow) icon at the top of the IDE. A little orange progress bar will appear at the bottom of the IDE, after which your Arduino will blink rapidly (indicating upload in progress), and then the little orange LED should start (or return to) blinking at a slow and steady pace.
As a final step with Blinky, you're going to edit the C++ code in the IDE window to blink faster. Since you can't overwrite a built-in sketch, you'll save a personal copy of Blinky: Do File / Save As ..., and save the sketch to your desktop. Although you probably haven't seen C++ code before, it should be obvious how to edit the sketch to make the LED blink faster. Once you've done that, show me your fast-blinking Arduino.
Each team has one such sensor, which you should now plug into your Arduino. Each sensor already has a set of jumper wires attached to it, each wire ending in a metal pin that you will plug into the appropriate place on the Arduino. (By convention, the term pin is also used to refer to the place on the Arduino where you insert the actual pin from the jumper.) So, connect the black (ground) wire to the GND pin on the Arduino, the red (power) wire to the 5V pin next to GND, and the green or blue (signal) wire to the pin marked A0.
As with Blinky, there's a pre-written sketch you can run to test your analog sensor: go to File / Examples / Basics / AnalogReadSerial. After uploading the sketch as you did for Blinky, you can display the values from your sensor by going to Tools / Serial Monitor. A new window should pop up, scrolling the values for your sensor. These will be “raw” values from the A/D converter, which normally we would then convert to meaningful units (degrees, millibars, centimeters, etc.) using additional code. For now it is sufficient to verify that you can see the numbers changing as you change the input to your sensor (move the flame closer, cover/uncover the light sensor, blow warm air on the heat sensor).
Because the Arduino IDE is running on your computer, the Arduino must be communicating with it over a serial channel; that is what you are monitoring with the Serial Monitor. You can also monitor a serial channel with a so-called “terminal emulator” program. Once such program is Python's miniterm script. To run this script make sure that your Arduino serial monitor is closed, and issue the following line in your terminal window:
miniterm.py /dev/ttyACM0You should see the same kinds of numbers scrolling by in the terminal window.
git clone https://github.com/simondlevy/RealtimePlotterYou should now see the RealtimePlotter folder on your desktop. Close the terminal window, open this new folder, and launch a new terminal from there. Now you can get a simple demo of the plotter by issuing the command
python3 slowfast.pyYou should see two signals scrolling by: a red dashed signal labeled Slow, and a solid blue signal labeled Fast. These signals are generated by the slowfast script using sine waves, as a simple way of making sure that the RealtimePlotter code is working correctly.
git clone https://github.com/tino/pyFirmataInside the new pyFirmata folder you'll see a pyfirmata folder. Drag this pyfirmata folder into your RealtimePlotter folder.
python3 chapter13.pyAlthough you may get some nasty warnings in the terminal window, you'll soon see that the script just plots some random values between 0 and 1. It also is labeled with the convention XXX to show you where code will need to be added or modified. Once you've got the code working, you should remove the XXX, and replace the comment with a typical comment (e.g., # Connect to the Arduino).
Based on the example on page 267 of the textbook, you should now be able to add / modify code in the XXX locations. Since PyFirmata can be a bit flaky, I made sure to check that the value returned from board.analog.read() was not equal to None, before setting the ycurr value to it. Note also that it typically takes a few seconds for pyFirmata to connect to the Arduino.
Whichever sensor you have, you will find it helpful to print out the values you're getting from it. This will help you figure out the minimum and maximum values for initializing the plotter. As a final step before showing me your demo, see whether you can find any online documentation about how to convert the raw sensor value to meaningful units (like degrees Celsius, lumens, etc.)