4. Embedded programming¶
what is micro-controller?¶
A microcontroller is a compact, single-chip computer designed to perform specific tasks in electronic devices and systems. It has a processor, memory, and I/O interfaces, making it versatile for embedded applications like IoT, robotics, and consumer electronics.
Group assignment¶
In the first session, we learned about what a microcontroller is. We were then asked to research different types of microcontroller boards and compare them. A microcontroller is a self-contained computer system on a single integrated circuit chip. It includes a processor, memory, and programmable input/output interfaces, allowing it to interact with connected devices and perform specific tasks. There are numerous types of microcontrollers, each with distinct features and applications.
Individual assignment¶
We had to program the board we have to do something.
The mirocontroller I had is Arduino Nano Every.
The Nano Every is Arduino’s 5V compatible board in the smallest available form factor: 45x18mm! The Arduino Nano is the preferred board for many projects requiring a small and easy to use microcontroller board. The small footprint and low price, make the Nano Every particularly suited for wearable inventions, low cost robotics, electronic musical instruments, and general use to control smaller parts of a larger projects.
Programming Platform¶
I worked with two main softwares: Arduino IDE and Thonny. We had a set of challenges to perform using these two softwares.
Challenge 1: Blink Coding
The first challenge was the “blink challenge”, which involved programming the microcontroller to blink its LED randomly at intervals between 1 to 5 seconds. For this challenge, I started with the built-in blink example in the Arduino IDE. I then modified the code to make the blinking intervals random instead of fixed. The code for this challenge is provided below.
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// Turn the LED on
digitalWrite(LED_BUILTIN, HIGH);
// Generate a random delay between 1 and 5 seconds
int delayTime = random(1000, 5001);
delay(delayTime);
// Turn the LED off
digitalWrite(LED_BUILTIN, LOW);
// Generate a random delay between 1 and 5 seconds
delayTime = random(1000, 5001);
delay(delayTime);
}
I also used Thonny software to do this challenge using the following code:
from machine import Pin
import time
import random
r = random.randint(1,5)
LED = Pin(26,Pin.OUT)
while True:
LED.value(1)
time.sleep(r)
LED.value(0)
time.sleep(r)
Challenge 2: Morse Code¶
The second challenge was to create a Morse code system using the blink effect, where each letter is represented by a specific number of blinks within a specific time frame.
I used Arduino software. Before starting, I planned to show the word “Teacher” in morse code. Based on the table above. The code used:
// the setup function runs once when you press reset or power the board
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(2000); // T
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000); // wait for a second
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // E
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000); // wait for a second
digitalWrite(LED_BUILTIN, HIGH); // A
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // A
delay(2000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000);
digitalWrite(LED_BUILTIN, HIGH); // C
delay(2000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // C
delay(1000);
digitalWrite(LED_BUILTIN, HIGH); // C
delay(2000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // C
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000);
digitalWrite(LED_BUILTIN, HIGH); // H
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // H
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // H
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // H
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000);
digitalWrite(LED_BUILTIN, HIGH); // E
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000);
digitalWrite(LED_BUILTIN, HIGH); // R
delay(1000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // R
delay(2000);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500);
digitalWrite(LED_BUILTIN, HIGH); // R
delay(1000);
}
I also used Thonny software to do this challenge using the following code:
from machine import Pin
import time
LED = Pin(26,Pin.OUT)
while True:
LED.value(1)
time.sleep(1)
LED.value(0)
time.sleep(0.5)
LED.value(1)
time.sleep(1)
LED.value(0)
time.sleep(0.5)
LED.value(1)
time.sleep(1)
LED.value(0)
time.sleep(3)# new
LED.value(1)
time.sleep(2)
LED.value(0)
time.sleep(0.5)
LED.value(1)
time.sleep(2)
LED.value(0)
time.sleep(0.5)
LED.value(1)
time.sleep(2)
LED.value(0)
time.sleep(3)# new
LED.value(1)
time.sleep(1)
LED.value(0)
time.sleep(0.5)
LED.value(1)
time.sleep(1)
LED.value(0)
time.sleep(0.5)
LED.value(1)
time.sleep(1)
LED.value(0)
time.sleep(5)