# Embedded programming: Arduino C / microPython ## Simulation Go to [Wokwi](https://wokwi.com/) and open a [new esp32 s3 starter template](https://wokwi.com/projects/new/esp32-s3) ## Arduino IDE Do you have the [Arduino IDE](https://www.arduino.cc/en/software/) with the esp32 boards package installed and a [Barduino](https://fablabbcn-projects.gitlab.io/electronics/barduino-docs/)? Then you can follow this physically. Remember to Enable "USB CDC on boot"! ## 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](https://learn.sparkfun.com/tutorials/data-types-in-arduino/defining-data-types) ### Arithmetic operators `+` `-` `*` `/` ### Example: variables and types Most commonly seen: `boolean`, `int`, `float`, `String` Example: ```arduino 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. ## 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);` ### Example: [Blink](https://docs.arduino.cc/built-in-examples/basics/Blink/) ```arduino 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. ## Conditionals ```arduino 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` ```arduino for (initialization; condition; increment) { // statement(s); } ``` ### `while` ```arduino while (condition) { // statement(s) } ``` # Barduino [Barduino @ Fab Lab BCN local docs](https://fablabbcn-projects.gitlab.io/learning/educational-docs/fabacademy/classes/04-EmbeddedProgramming/#barduino-40) ## [Programming Barduino with the Arduino IDE](https://fablabbcn-projects.gitlab.io/learning/educational-docs/fabacademy/classes/04-EmbeddedProgramming/#arduino-programming) # MicroPython ## Installing MicroPython on Barduino We are going to use [Thonny](https://thonny.org/), a simple Python IDE. Open your terminal. Make and change directory to where you want to keep your Python files ```bash uv init uv add thonny esptool uv run 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 ```python from machine import Pin` buzzer_pin = Pin(14, Pin.OUT) button_pin = Pin(11, Pin.IN) ``` ## `delay` `from utime import sleep` ## Buzzer ### Arduino: ```arduino tone(buzzer_pin, frequency, duration); ``` ### uPython: ```python 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](https://learn.sparkfun.com/tutorials/data-types-in-arduino/defining-data-types) [Tones with PIO on the RP2040](https://www.instructables.com/Respberry-Pi-Pico-W-Generating-Tones-With-Programm/) [MicroPython on the ESP32](https://docs.micropython.org/en/latest/esp32/tutorial/intro.html) [What is "Scope" in an Arduino program?](https://forum.arduino.cc/t/what-is-scope/369179)