Try   HackMD

Embedded programming: Arduino C

Simulation

Go to Tinkercad and open a new blank circuit project.

Example: a simple LED circuit

LED + resistor (always resistor!)

Anatomy of an LED

Long pin connects to +, short to -.

Exercise: a circuit with an LED and a button

Add a button that controls the LED in the circuit we just made. Where does the button have to be connected?

Solution

Solution in TinkerCAD.

Example: anatomy of an Arduino sketch

setup().

loop().

Communicating with a board

Serial.begin(115200);

Serial.print("something");

Serial.println("something else");

Exercise: println

Make the board call your name!

Arduino types

Arithmetic operators

+

-

*

/

Example: variables and types

Most commonly seen: boolean, int, float, String

Example:

void setup() {
  int resultAsInt = 4.3 + 3.4;
  float resultAsFloat = 4.3 + 3.4;
  
  Serial.begin(115200);
  Serial.println(resultAsInt);
  Serial.println(resultAsFloat);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Exercise: Arduino C arithmetic

Write code to calculate:

  • 7 times 4.2.
  • 8 divided by (3 + 2)

Controlling time

delay(x)

millis()

Exercise: counting time

write code that counts the time in seconds that has passed from the boot of the board and writes it out to screen, repeatedly.

If it runs too fast, delay it!

Exercise: conting loop executions

Write code to count how many times the loop function has run. include it in the preceding sketch.

Solution to the last few exercises

Solution in TinkerCAD.

Digital Input / Output

This is the magic. This sets apart coding for a microcontroller from coding for a computer.

Output

digitalWrite(pin, value); where value is either HIGH or LOW

Input

value = digitalRead(pin);

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

Reading buttons

Pull up/ pull down resistors.

Exercise

Detect if a button is on or off.

Solution

Solution in TinkerCAD

Conditionals

if(condition) {
// run if true
} else {
// if false
}

Boolean operators

!

==

!=

>

<

Exercise: detecting button presses

Checking if a button is on is easy, but can you count how many times it has been pressed?

Loops

for

for (initialization; condition; increment) {
      // statement(s);
    }

while

while (condition) {
      // statement(s)
    }

Barduino

Barduino @ Fab Lab BCN local docs

Programming Barduino with the Arduino IDE

MicroPython

Installing MicroPython on Barduino

We are going to use Thonny, a simple Python IDE.

pip install thonny

Then

  1. Open settings
  2. interpreter tab
  3. Choose MicroPython (ESP32) from the dropdown
  4. Select the Barduino port from the dropdown
  5. Click install or update MicroPython
  6. Just in case, restart the Barduino

Setting pins to input/output

from machine import Pin`

buzzer_pin = Pin(14, Pin.OUT)
button_pin = Pin(11, Pin.IN)

delay

from utime import sleep

Buzzer

Arduino:

tone(buzzer_pin, frequency, duration);

uPython:

from machine import PWM

PWM(buzzer_pin, freq=frequency, duty=512)
sleep(duration)

Exercise

Make your board sing! Choose a melody and play it on the Barduino.

References

Data Types in Arduino

Tones with PIO on the RP2040

MicroPython on the ESP32

What is "Scope" in an Arduino program?