Reconfigurable computing
Small Embedded Systems
Unit 1.5
Introduction to Arduino Coding
Arduino Coding
Arduino software environment is based on C++, but with simplified programmer interface
Library of functions that tries to give generic view of different microcontrollers and boards, so that programmer doesn’t have to worry about the details of different systems
Strong user community and ecosystem of software libraries
Arduino IDE
Integrated Development Environment (IDE)
Front-end to a C++ compiler
User program is called a “sketch”
Arduino IDE
Minimal program consists of
Header: declarations, #include, #define, etc.
Setup() – function run once at start up (or reset)
Loop() – function runs repeatedly
// Declarations go here
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
A simple program
Most development boards have an on-board LED
Simple program will blink the LED
const int LED_PIN=0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
Declarations of global variables
Runs once at startup and reset
Runs repeatedly
A simple program
Most development boards have an on-board LED
Simple program will blink the LED
const int LED_PIN=0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
For Huzzah, it’s pin 0
For Uno, Nano, it’s pin 13
The pin connected to the LED
A simple program
Most development boards have an on-board LED
Simple program will blink the LED
const int LED_PIN=0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
It’s a good idea to tell the compiler that this won’t change during program execution
That means it can be placed in non-volatile memory
Volatile memory is a scarce resource on many micro-controllers
A simple program
Most development boards have an on-board LED
Simple program will blink the LED
const int LED_PIN=0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
This is a function from the Arduino library
Sets the specified pin to be an output
A simple program
Most development boards have an on-board LED
Simple program will blink the LED
const int LED_PIN=0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
Another function from the Arduino library
Constants defined in the Arduino library
A simple program
Most development boards have an on-board LED
Simple program will blink the LED
const int LED_PIN=0;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
Another function from the Arduino library
delay() time is measured in milliseconds
A simple program
Here’s what it looks like when it runs
Uno
Tinkercad
What happens when we compile?
Compiler takes user’s sketch
Automatically generates function prototypes for any functions defined in sketch
Automatically incorporates headers that represent the hardware board chosen
Incorporates it into this framework
// declarations and definitions for library are
// automatically incorporated here
int main(void){
init(); // Set-up everything ready
setup(); // Run function setup() from user’s sketch
while(1) // Infinite loop
loop(); // Repeatedly run loop() function from sketch
return 0;
}
Is our simple Program any good?
No – the code uses a “busy wait”
The processor is kept busy all the time executing this code
It can’t do anything else useful
Later we’ll see ways to get the blink without tying up the processor
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
Reading Data In
Connect a button to pin 6
Turn LED on and off using button
const int LED=0;
const int BUTTON=6;
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop() {
if (digitalRead(BUTTON) == LOW) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
To pin 6
0 V
3.3 V
Return value is LOW or HIGH
Communicating with Host Computer
Original PCs used serial port, using a DB9 connector
Communicated using RS232 standard for serial comms
Appears to MS Windows as COM port (COM1, COM2, …)
Appears to Unix/Linux as /dev/ttyS1, /dev/ttyS2, …
Although these connectors and physical interfaces are rare nowadays, “virtual serial ports” are commonly used to connect to embedded systems and industrial control equipment
Virtual Serial Ports
Most Arduino development boards have an on-board converter chip so that we can use serial port communications over USB
Serial port is asynchronous
It doesn’t send the clock data
Both ends of communication need to know in advance what transfer speed (“baud rate”) is being used
Communicating with Host Computer
Code to communicate with host computer
const int LED_PIN=0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(“Turning LED off”);
digitalWrite(LED_PIN, HIGH);
delay(1000);
Serial.println(“Turning LED on”);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
Set up serial connection at 9600 bps
Print message to host computer terminal (terminate with newline)
Communicating with Host Computer
Terminal on host computer
Set up serial connection at 9600 bps
Communicating with Host Computer
Terminal on host computer – mismatched baud rate
Mismatch of baud rate gives garbage
void loop() {
Serial.println(“Turning LED off”);
digitalWrite(LED_PIN, HIGH);
delay(1000);
Serial.println(“Turning LED on”);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
Communicating with Host Computer
By the way, on Feather Huzzah, pin is connected to cathode of indicator LED
Turned on by LOW and turned off by a HIGH
That’s the opposite to Arduino Uno and Nano boards, which are used in most code examples on the Internet
Huzzah board
void loop() {
Serial.println(“Turning LED off”);
digitalWrite(LED_PIN, LOW);
delay(1000);
Serial.println(“Turning LED on”);
digitalWrite(LED_PIN, HIGH);
delay(1000);
}
Communicating with Host Computer
By the way, on Feather Huzzah, pin is connected to cathode of indicator LED
Turned on by LOW and turned off by a HIGH
That’s the opposite to Arduino Uno and Nano boards, which are used in most code examples on the Internet
Nano, Uno boards
Communicating with Host Computer
We can send content of variables to terminal
Code example whose blink delay gradually increases
void loop() {
for (int i=0; i<10; ++i) {
int delayTime = (i*100);
Serial.print("Flipping LED state. \tDelay is ");
Serial.println(delayTime);
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
delay(delayTime);
}
}
No line break after this print
Read current status of pin
! is NOT operator in C
\t is tab
Communicating with Host Computer
We can send content of variables to terminal
Code example whose blink delay gradually increases
void loop() {
for (int i=0; i<10; ++i) {
int delayTime = (i*100);
Serial.print("Flipping LED state. \tDelay is ");
Serial.println(delayTime);
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
delay(delayTime);
}
}
Communicating with Host Computer
We can send content of variables to terminal
Code example whose blink delay gradually increases
Summary
Arduino ecosystem aims to make embedded programming accessible
Software development environment has libraries to give simplified and easy control over I/O and timer functions
Lavf57.71.100
Lavf57.71.100
Lavf57.71.100
/docProps/thumbnail.jpeg