Go to Tinkercad and open a new blank circuit project.
LED + resistor (always resistor!)
Long pin connects to +, short to -.
Add a button that controls the LED in the circuit we just made. Where does the button have to be connected?
setup()
.
loop()
.
Serial.begin(115200);
Serial.print("something");
Serial.println("something else");
println
Make the board call your name!
+
-
*
/
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:
}
Write code to calculate:
delay(x)
millis()
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!
loop
executionsWrite code to count how many times the loop
function has run. include it in the preceding sketch.
This is the magic. This sets apart coding for a microcontroller from coding for a computer.
digitalWrite(pin, value);
where value is either HIGH
or LOW
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
}
Pull up/ pull down resistors.
Detect if a button is on or off.
if(condition) {
// run if true
} else {
// if false
}
!
==
!=
>
<
Checking if a button is on is easy, but can you count how many times it has been pressed?
for
for (initialization; condition; increment) {
// statement(s);
}
while
while (condition) {
// statement(s)
}
Barduino @ Fab Lab BCN local docs
We are going to use Thonny, a simple Python IDE.
pip install thonny
Then
settings
interpreter
tabMicroPython (ESP32)
from the dropdownfrom machine import Pin`
buzzer_pin = Pin(14, Pin.OUT)
button_pin = Pin(11, Pin.IN)
delay
from utime import sleep
tone(buzzer_pin, frequency, duration);
from machine import PWM
PWM(buzzer_pin, freq=frequency, duty=512)
sleep(duration)
Make your board sing! Choose a melody and play it on the Barduino.