# Embedded programming: Arduino C ## Simulation Go to [Tinkercad](https://www.tinkercad.com) 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](https://www.tinkercad.com/things/lCAGHGM8jXU-my-first-circuits?sharecode=2R3akAn6U5BR25t1L-vzLE8fayWBLAxGAw1l5EeOi3g). ## 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. #### Solution to the last few exercises [Solution in TinkerCAD](https://www.tinkercad.com/things/6qJhJ6jxWYw-my-first-arduino-sketch?sharecode=Mqfvc3sW2umIT6z8Puzb9zO0tHoAk_0xDVn370A6ANw). ## 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. #### Solution [Solution in TinkerCAD](https://www.tinkercad.com/things/4tr8Vg52H38-led-on-arduino-and-pushbutton-pulled-up?sharecode=RfEANrMjWPHY7Gps8UlMl1zHaH5MrPLFUma-Pifx95I) ## 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. ```bash 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 ```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)