CSCI 250 Lab #1: Getting Started with Arduino
Goal / Turnin
The goal of this lab is to get familiar with Arduino and simple sensors. Arduino is a microcontroller; that is, a computer that runs one program at a time – called the sketch, firmware, or firmware sketch – allowing the computer to focus all its efforts on interacting with sensors and other devices. We will also set up a private repository that you and your team members can share for this lab and the rest of the course.
At the end of the lab session you will demonstrate your program to me. Then one of your team members will upload your code to your team’s github repository, so that the entire team can get credit for completing the lab.
Part 1: Run Blinky on Arduino
As “Hello world!” is to introductory programming classes, “Blinky” is to Arduino. Although there are many kinds of Arduino (the most basic being the Arduino Uno we’re using in this lab), every Arduino comes with at least one LED, a tiny light that can be used to indicate the status of your program. Blinky is the nickname for the Blink sketch that allows you to test the LED, by turning it on and off at regular intervals.
As with IDLE3 for Python, Arduino has its own Integrated Development Environment (IDE). If you and your team prefer to work from a laptop, feel free to install the Arduino IDE on your laptop; otherwise, to launch the Arduino IDE from a classroom machine, open a terminal window from your desktop and type /opt/arduino-1.8.19/arduino. If you prefer to work on the classroom computers, I can also help you set up a little desktop launcher for Arduino to avoid having to type this command every time.
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 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 something like Port: ________ (Arduino/Genuino Uno) . 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. (Arduino is also a fun way to get started in the popular C++ language!) Once you’ve done that, show me your fast-blinking Arduino.
Part 2: Reading an displaying an analog sensor signal using Arduino
The simplest kind of signal you can get from a sensor is an analog signal. Unlike the digital signals we’re studying in class (TTL, I2C, USB), an analog signal has a smoothly-varying voltage corresponding to the quantity you’re sensing (light, temperature, pressure, etc.). The Arduino’s built-in analog-to-digital convert (a.k.a. A/D Converter, or ADC) converts such a signal to a number that can be displayed or used in the sketch.
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).
Finally, the Arduino IDE allows you to plot values instead of printing them out. Try out the Tools / Serial Plotter selection and show me what you get!
Part 3: From sensing to acting
As we learned in our “Be the Robot” exercise, even the simplest robot consist of a sensor, an actuator, and a control algorithm that takes actions based values read from the sensor. For the final part of this lab exercise, I will give you a small servo motor, and your goal will be to make the motor move based on the value that the Arduino is reading from your sensor.
Looking at the built-in Examples menu, I didn’t see anything about servos, but after a little googling I found this nice tutorial. As before, I recommend trying out the simplest example (Sweep), to get a feel for how the servo works. Then you can copy/paste/modify the Knob example to work with your sensor instead of the potentiometer used in the example. See what kind of sensing/actuating demo you can come up with, and show it to me when you finish.