School of Physics & AstronomySchool of Physics & Astronomy
Programming with the
Arduino: open-source
hardware in an introductory
programming laboratory
Paul Cruickshank [email protected]
Motivation
Prior to 2013, no formal programming for St Andrews physics students until 3rd year
Earlier programming experience thought desirable
Aims to introduce 3 key concepts:
loops, decision making and functions Available time: 7.5 hours over three weeks
School of Physics & AstronomySchool of Physics & Astronomy
Context: computing in the St
Andrews physics degree
2nd year: some Python in modules taught by School of Maths
2nd year: 7.5 hours of C in Arduino lab (all physics/astrophysics students)
3rd year: Computational Physics module,10 credits, Mathematica (all physics/astrophysics students)
3rd year: Computational Astrophysics, 15 credits,
Fortran
3rd year: 15 hours of LabView as part of physics
The Arduino (UNO R3)
Atmel ATmega328 8-bit microcontroller, 16MHz
14 digital I/O pins
6 analogue input pins (10-bit)
5V supply via USB, 7-12V external, on-board 5V and 3.3V regulators
HUGE community of users
£21.66 inc VAT (Farnell)
School of Physics & AstronomySchool of Physics & Astronomy
The Arduino IDE
Java based, so platform agnostic*
Programs written in C/C++ Hardware functions
abstracted away from hardware pretty well Core function set pretty minimal, but easy to learn
The Arduino IDE
All Arduino programs MUST contain TWO functions,
called ‘setup’ and ‘loop’ When program compiled, IDE adds these and other code to generate a ‘proper’ C++ file, then compiles that
School of Physics & AstronomySchool of Physics & Astronomy
Why use Arduino?
Replaced part of an electronics
practical, so wanted to retain practical feel
Very easy to interface to
Built-in analogue to digital converter Digital input/output
Libraries and examples for most things you can think of (not always a good
Good things about the Arduino
Programmed in C/C++
Low barrier to entry (cost, availability, computer requirements)
Exercises have a good practical feel:
easy to write programs that interact with outside world
School of Physics & AstronomySchool of Physics & Astronomy
Less good things about the
Arduino
(Deliberately) not designed for teaching programming
Programmed in C/C++
Compiler error messages less than transparent
Development environment can be restrictive Occasional driver issues
No console (although serial monitor works out ok)
Development
Spring 2012: final-year student project devising trial script (Adam Hollan)
Single afternoon trial run with 9
volunteers from intended target group All student questions to demonstrators recorded
Spring 2013: final-year student project evaluating first live run (Duncan
School of Physics & AstronomySchool of Physics & Astronomy
Lab structure
Three 2.5 hr afternoon sessions
Before starting, students answer pre-lab questions based on content of lab script
Lab afternoon one
Introduction to environment Introduction to syntax
Analogue input: start with a potentiometer (dull but straightforward), on to thermistor, LDR and analogue accelerometer
School of Physics & AstronomySchool of Physics & Astronomy
Lab afternoon two
Analogue output (using external DAC, with pre-written library functions)
Decision making (i.e. if and if…else statements)
Introduction to flowcharts Loops: for and while
Resistive touchscreen sensor (with pre-written library functions)
Lab afternoon three
Write and use program to measure the IV characteristic of a diode
Develops the automation of an experiment students will already have done by hand
Designed to link in with previous work and make use of experience of first two
afternoons
Also shows utility of automation of measurement tasks
School of Physics & AstronomySchool of Physics & Astronomy
Lab supporting materials
Script entirely self-contained
Include text of sample programs so that students can read in advance
Extensively footnoted to point out
parallels and differences with standard C
Lab supporting materials
Try hard to separate programming aspect from electronics aspect Provide suggested
breadboard layouts for exercises: found some students get bogged down in assembling circuits, leaving less
time for the main point of the lab
School of Physics & AstronomySchool of Physics & Astronomy
Demonstrator training and
support
Demonstrators work independently through lab few weeks before start
Deliberate move to develop familiarity Also uncovers errors in script/libraries and ensures manageable load for lab time (7.5 hours)
Typically ~3.5hrs for no C/C++ prior experience
Demonstrator support
4 demonstrators for ~30 students
Demonstrators encouraged to be pro-active
More than half required no time outside scheduled periods
Of those who did, an hour was adequate
School of Physics & AstronomySchool of Physics & Astronomy
Approx. costs for 40 sets (inc.
VAT)
Arduino boards & cables: £880 Touchscreens: £330 Accelerometers: £330 PCBs: £400 Other components: £400
Most of these one-offs, less than £100 per year
Difficulties
Mark distribution is poor, and marks generally high (~80%)
Tension between teaching a skill and having to put a number on the end
result
Lab doesn’t discriminate amongst the most capable students effectively
School of Physics & AstronomySchool of Physics & Astronomy
Evaluation (2013)
Comprehensive questionnaire
Recruited volunteers to record audio of their lab experience along with their
written text, using LiveScribe pens Most surprising sources of difficulty
related to experimental technique rather than programming
Student reception (2013
evaluation)
Overall, pretty positive: they didn’t hate it, and no-one died
Asked students to respond to a series of questions or statements on a five-point Likert scale
School of Physics & AstronomySchool of Physics & Astronomy
Feedback
How difficult did you find this lab compared to other second year labs?
(N=79)
Feedback
How enjoyable did you find the Arduino lab? (N=79)
School of Physics & AstronomySchool of Physics & Astronomy
Feedback
This lab opened up a whole new area of physics to me
(N=77)
Feedback
Overall impression: (N=78)
School of Physics & AstronomySchool of Physics & Astronomy
Feedback
Overall impression: perceived gain from the lab
(N=78)
Example applications from
elsewhere
10 by 10 temperature sensor array Eric Ayars, California State
University, Chico
(Very instructive blog, http://hacks.ayars.org/)
used with permission
School of Physics & AstronomySchool of Physics & Astronomy
Example applications from
elsewhere
used with permission
Orthopaedic rehab studies using Arduino based data acquisition systems of
accelerometer and goniometer data
obtained from the knee Graham Brooker, AFCR, University of Sydney
Our plans for Arduino
From spring next year: take-home lab Give all students a box with necessary kit
Keep same amount of available contact time
If successful, consider adding extra content
School of Physics & AstronomySchool of Physics & Astronomy