19 Arduino Basics
Many of these features have the same or similar behaviours in other environments. This section on the basics is intended to get you started in programming with the Arduino IDE, writing code in C with calls to Arduino specific functions. It covers a lot of the same elements as the Learning Sequence code and videos, however it is arranged to highlight issues that have come up repeatedly working with MECH 217 students.
The code is intentionally not included for cutting and pasting. You will retain the ideas better if you type the code as explained in the videos, and will be less likely to cut and paste without understanding. That kind of cutting and pasting is responsible for most of the errors that pop up in student code.
Start by Remembering C Language Basics
I still like the original The C Programming Language by Kernighan and Ritchie, who wrote the language. It’s remarkably readable, especially the first chapter tutorial introduction. Not many books get their own Wikipedia page. https://www.learn-c.org/ provides an online tutorial with similar content. https://www.onlinegdb.com/ provides online access to the GDB debugging environment where you can track execution of your C code. If you already know C, then you know most of what you need to program the Arduino UNO. The framework is different but the language is C. C++ is used to write the libraries and a little of that trickles down to your sketches. The definitive reference for the Arduino language and libraries is at https://www.arduino.cc/reference/en/
setup() and loop() functions
An Arduino sketch runs the setup() function only once, then runs the loop() function over and over forever. It never stops.
This video (1:37) uses Serial.print()
and delay()
to help see that behaviour. The micros()
and millis()
functions help keep track of time.
Declare, Initialize, and Use Variables
A variable declared outside a function is global (visible in all functions). If it includes an initialization, that initialization happens only once at the start of execution.
A variable declared inside a function is local (visible only in that function). If it includes an initialization, that initialization happens every time the function is called, and happens in the sequential order of execution.
This video (3:08) uses Serial.printf("%8.3f, %9d, %9u\n", x, i, t)
to reduce the complexity of print formatting for numerical values. It is important to make the format strings match the variable types for float, int, and unsigned variables.
Sequential Execution, TOP to Bottom
In the absence of other control structures, like if()
, while()
, for()
, each statement in a function is executed in sequence from top to bottom. This is different from a spreadsheet, where the application makes sure to execute all the formulas in the right order to resolve any conflicts. It is important for all calculation steps to happen in the correct sequence. (video 1:20)
Use a for Loop to Iterate
for()
loops will be a major feature in your code, allowing you to repeat something multiple times as you would to average some input values, or to iterate over multiple values in an array or list, as shown in this example. (video 5:51)
Define and Call a Function
This video (1:52) shows how to define and call a function to do a task or calculate a value.
Save a Value in a Global Variable
Although C supports the idea of “static
” variables that are local to a function, the simplest way to store a value between function calls is with a global variable. By saving a value at the end of a function, you can recall that value next time the function is called. This is useful for keeping track of time during execution of your sketch. You can also use global variables to share values between multiple functions without needing to pass them as arguments. Computer scientists often object to global variables, so use them with caution. (video 1:01)
Curly Braces {}
Group Statements Together
They define the beginning and end of the code for a function, or the collection of lines that are referred to by a control structure like an if()
statement. Use Control-T or Command-T to reformat your code to set the indentation properly and help find a mismatch.
The video (1:30) shows how to use an if()
statement to print less often than every time through the loop.
Digital Input and Output (DIO or GPIO)
With a pushbutton connected between pin 12 and ground we can get user input by reading pin 12. We can use that input to control how we blink the LED that’s attached to pin 13 of almost every Arduino compatible microcontroller.
The video (3:15) shows how to set pinMode()
and then use digitalRead()
and digitalWrite()
to interact with physical pins.
Pulse Width Modulated (PWM) Output
Some digital pins allow PWM output to simulate an intermediate voltage by rapidly switching between high and low values.
The video (2:11) shows how to use PWM to dim the LED on pin 13 with analogWrite()
.
Analog Input (ADC) and Output (DAC)
All Arduino compatibles allow analog input, typically 10 bits or better on pins A0 to A5. More advanced microcontrollers also support higher bit resolutions and true analog output on a subset of pins.
The video (2:55) shows how to write analog values to the special DAC pin A0 with analogWrite()
and read them back in on pin A3 with analogRead()
.