Arduino
Arduino
Discussion stems from a UMass course, Physics 192M, taught in spring 2018-2020.
All course materials are available; please email dinsmore@physics.umass.edu
Why design a new course?
Our Physics BS program provides a powerful skillset in problem solving, analytical thinking, model
building, quantitative analysis. (Also, writing, presenting, some computational skills.)
Can we do this in a classroom setting? We’re trying a 1-credit course for UMass freshmen. Optional.
• Provide these experiences in the context of science and exploring the unknown.
• Defining effective science questions is foundational in science research.
• Successful parts (if any) to be incorporated into lab-course curriculum.
• Long run: incorporate makerspaces into the curriculum to provide 4-year support.
Physics192M: Introduction to measurement using the Arduino
By the end of the course, students will have defined their own question, set up a device to collect data, and
answered the question. The course also provides a first experience (for many) in creating a logical sequence of
steps to carry out a task, in preparation for programming. No prior experience with computing or hardware is
needed.
Key skills to be developed:
(i) Defining a curiosity-driven question in a constructive way.
(ii) Designing, making, and trouble-shooting an apparatus to measure a physical quantity.
(iii) Facility with the hardware-software interface.
(iv) Confidence in completing a technical task independently.
Comments for potential instructors:
• 1 credit. One 2-h meeting plus 1-2 h homework/week.
• Pre-req: Intro-level mechanics; that’s it.
• 1 faculty instructor + 1 TA for up to 20 students.
• No textbook but each student should plan $20 for parts.
• Be ready to quietly help students with $$.
• This year, we bought 40 Arduinos and loaned them.
• Don’t be anxious about getting stumped on technical Qs.
www.adafruit.com
The pins of an Arduino (or Elegoo) Uno
Voltage (V)
Dry, 1100
Mark
Lewis, charged 1000
BS 2017 sand Lower
900 humidity
800
Acquisition
700
(Several details added near the end – where to get help, saving/exporting data, some details on Tony’s class, etc)
Example questions from Phys192M
• How does GPS location on campus affect the connection strength of the eduroam wireless
network?
• How does ambient noise change based on what part of campus you are in?
• How loud does my ukulele have to be in order to hear it my suitemates’ room?
• How does the volume of my room when my neighbors aren't home compare to the volume of my
room when my neighbors are making noise?
• As I take a shower, how do temperature and humidity of the bathroom correlate as function of
time?
• How does the weather affect the amount of people that are signed into my residential hall?
• How long does it take for my plants to dry out to the point where they need another watering?
[Several plant-related!]
• What is the acceleration rate and top speed of my RC car?
• How much movement do I make in my sleep compared to when laying perfectly still in bed?
• What is my average maximum exhalation time for blowing a note into my flute, and does this
vary with pitch?
• How does the air quality of my room [CO2, VOCs] change over the course of the day?
• How long does my dog sleep on my bed while I am away?
Posing an effective science question
Feb 24 April 17
How does temperature affect the amount of people How does the weather affect the amount of people
who enter and exit residential buildings? that are signed into my residential hall?
How loud are my neighbors at night? How does the volume of my room when my
neighbors aren't home compare to the volume of
my room when my neighbors are making noise?
Is there a point in the day when the room is empty? For how long is my room empty of people?
How does the sound level in my room vary When is the best time in my room to study?
throughout the day?
How can I make an RC car move with a battery, what is the acceleration rate and top speed of my
electric motor, a remote, and an additional arduino RC car?
as the motor controller?
Questions about the habits of individual people other than the experimenter are not allowed.
8 + 20 min
(We can
What makes an “effective science question?” certainly take
more time if
desired.)
(This
We’ll measure DV.
We’ll use the pre-installed program called “AnalogReadSerial.” (Details on next slides)
(We used AnalogReadSerial, but I think that it’s easier to start with ReadAnalogVoltage. The
reason is that the latter does a little calculation to make the reported value into a real voltage.
AnalogReadSerial gives an integer value on a linear scale from 0 1023.)
You are welcome to use a UMass laptop
If you have your own laptop and want to install the Arduino software, go to:
https://www.arduino.cc/en/main/software (or Google “Arduino IDE”)
• The “web editor” works but is not ideal for repeated use.
• Download and install Arduino 1.8.10. (Windows, Mac or Linux)
• Don’t use the “Windows App” link for a laptop or desktop.
The Arduino IDE
The IDE converts your commands into machine
code- it’s an interface. The language is like C++,
but if you’ve never programmed you can get
started fairly quickly by copying example code.
There is a vast array of helpful resources online.
(or ReadAnalogVoltage; see slide 16)
Let’s get started: Run “Arduino.exe.” Menu: file…
Examples…01.Basics…AnalogReadSerial. It should
look like this (see at right):
Click on the arrow to compile your code and
upload it to the Arduino. If it fails:
(a) Click on the check button next to it. If you have
a syntax error, you’ll see orange text at the
bottom of the screen.
(b) If it compiles OK, you might have a “USB port”
error. The most common errors are fixed by
Menu: Tools…Board, or Tools…Port. You may
have to do this many times.
This message is good news: the code was compiled and uploaded.
Click here to open a window
that shows the data coming
You can add comments (notes to from the “Serial.println”
yourself) between “/*” and “*/” . Or, on command.
any line after “//”. Do yourself a favor
and write many detailed comments!
pin A0
+5
R approx. 10 kW
Measuring temperature
Temperature, TMP36 sensor; $1.50 from Adafruit
to A0 The code:
to 3.3 V or 5V pin to GND Start with “AnalogReadSerial.” Converts
1) Find this line: “sensorValue” to a
calibrated voltage
Writing
side
Here is the simplest way to export data to answer a question like this: Open up the IDE serial window. When you are
ready to start collecting data, press the “Clear output” button on the serial monitor window. Turn off autoscroll.
When you’ve collected enough data, select all (crtl-a) and copy and paste into a spreadsheet such as Excel. Then you
can manipulate the data, plot it, etc.
For two-column data (such as temperature and time), you can print the data in each row with commas (see next
slide). For Excel: copy/paste the data into a .txt file, and then import it with Excel. Try this with test data first; you
might have to change the format to make it work. For Google sheets: copy/paste directly into a single column of a
GoogleSheets spreadsheet, look for the little clipboard icon, and select “Split text to columns.”
Here’s how you could print data with each row having multiple data points separated by commas:
Serial.print(variableA);
Serial.print(",");
Serial.println(variableB);
Serial.print (without the ln) prints and remains on the same line.
Serial.println prints, and then makes a new line.
Try it. There’s also an example on next slide.
Adding time (not discussed during the workshop but some of you asked about it)
/*Modified by Tony Dinsmore, Jan. 2020, starting from ReadAnalogVoltage.
Lines were added to report the time as well as voltage in format appropriate for comma-delimited
data file. You can copy the serial window results and paste them into a Wordpad file and
save as "text.dat" and then import that file into Excel as 2 columns of data.
If you press the reset button on the board, you will see the program and the time start again.
*/
/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage
*/
To collect measurements of two or more parameters, //First, define some variables. (This could also be done in loop())
unsigned long t; // Need this for time. Unsigned long can hold a large value, which is
it’s easiest to print them in a comma-delimited format //necessary because our program might run for a large # of milliseonds!
double ts; //We will use this for the time in seconds. Double precision is used so that the
using the Arduino. Then you can export to Excel or // number of seconds can be large.
Google sheets, etc. The following program will do // The setup routine runs once after uploading the program or after pressing the reset button:
void setup() {
this. From your IDE: file…New.. And them remove // initialize serial communication at 9600 bits per second:
Serial.begin(9600);
everything in the window, and then copy/paste the }
Serial.println("Time (seconds), Voltage (V)");
text below into it. // The loop routine runs over and over again forever:
void loop() {
// First, read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Note the time, measured from when the Arduino started running this program:
t = millis(); //The variable t was defined in setup()
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
//Next, convert the time to seconds. The variable ts was defined in setup().
ts=t/1000.;
• What groups shall we form? (By grade level? By type of science? By…?)
• Divide into groups
• For 3 minutes, discuss “How can we teach these skills in a classroom?”
(Which skills? What projects? What aspects to emphasize?)
• Choose a note-taker.
• Report back to the group as a whole.
How can we teach these skills in a classroom?
Tony’s week-by-week schedule, 2019
W01: Get started with the Arduino. Measure voltage, measure temperature.
W02: Go further with temperature. “What happens to the temperature in my room when I am asleep?”
Sketching imaginary data. (symbols; never curves); arithmetic in the code; “millis()”, exporting data to spreadsheets for
plotting. HW: measure and plot T(t) and discuss.
W03: Measuring light intensity with the phototransistor. Coding logic-based decisions. Control LEDs. Voltage, current, and
Ohm’s Law. HW: set up the device, plot the data.
W04: Loops. Mean, standard deviation, standard error of the mean. HW: write a code to do this.
W05: Start working on final project. More loops; libraries. HW: write two questions, list necessary devices.
W06: Coding tips and pitfalls: matching data types, arrays, “=“ as assignment operator, functions, memory capacity of the
Arduino. HW: choose one question and refine it; sketch imaginary data; refine design.
W07: More on coding tips & memory. HW: refine the question and parts list.
W08: Correlations in data. HW: show preliminary data and write down two conclusions from it. If data not available, sketch
imaginary data and discuss.
W09: Schematic diagrams and wiring diagrams. HW assignment: make one of each.
W10: “What makes a science question effective?” HW10: refine question, continue working on project. List 1-2 technical
hurdles remaining.
W11: In class: work on project. Review hurdles with instructor. Leave early to collect data if approved.
W12: “ “ “
W13: In-class presentation
Finally, a written document.
Things done well & not so well in P192M
Things done well, mostly after learning from first time teaching:
• Students like designing their own project.
• Students like learning skills they think will be useful outside the class.
• The tone in class is constructive and stimulating. Students help one another.
• In class, when instructors review work that students posted for assignment, it is always constructive.
• Timing has improved:
• We get started with data acquisition and plotting fast.
• A staged sequence of mini-projects (temp, light…could use one more?)
• A staged sequence of specific coding skills across a few weeks. ← response to student comments
• Handouts with distilled programming tips are useful. There’s too much info online, not all of it relevant.
• Schematic and wiring diagrams are a good and surprisingly challenging exercise. Could be done earlier.
• 2-hour in-class period much better than 1.25-hour, which is far too short! ← response to student comments
• As many as 20 students in a section is OK with a good teaching assistant; 7 is a bit small.
The course “allowed [a student] to go above and beyond what I was capable of doing, and FAIL. Failing and
switching the basis of my project without an extreme impact to my grade was beyond valuable to me. “
Schematic diagram Wiring diagram
Computer
Arduino Uno
Phototransistor
Voltage USB to (Adafruit #2831)
detector computer
Power Anolog in
+5 V 5V GND A0
Resistor, 10 kW
Light
sensor
10 kW
What are the main parts and their What wire goes where?
function? (Not technical specs or names.) What are the names and (maybe) technical specs?
How does the information or current flow? It might be harder to see how the information or
Don’t forget the computer. current flows.
Things to be improved