4. Embedded programming¶
During this week we were introduced to embedded programming, we used different microcontrollers and different environments to experiment the different between them. Same as las week, we had a group assignment and indiviual assignment.
Task 1 (group assignment)¶
You can view our group assignment by clicking on the link below: Group Assignment
Task 2 (indiviual assignment)¶
Indiviually, we were asked to use two different environments to do 3 challenges. So we used Arduino IDE and MicroBlocks. I will start by presenting how to use each software and then I will present the challenges.
Arduino IDE¶
Arduino IDE is one of the most widely used software for coding, this is because it is simple, easy and suitable for beginners. In order to use it, you have to download it from this website. You just have to choose your prefered option
Once you open the application, you will get this interface
At the bottom right of the above image in the rectangle, it is shown that the microcontroller is not connected. So the first thing we learned is how to connect the microcontroller. To do that, we have made a short research to figure out the name of the microcontroller and how to connect it in Arduino IDE. So, I have used two different microcontrollers. First I used Seeed studio xiao esp32c3 and then Kidsbits ESP32. So, to connect the xiao esp32c3 I followed the steps provided here.
Step 1 is already done which is downloading Arduino IDE and launching the app
Step 2 is adding ESP32 board package to my Arduino IDE by clicking on File > Preferences > paste this code https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.jsons in Additional Boards Manager URLs
Step 3 is installing the latest version of esp32 by clicking on Tools > Board > Boards Manager
Step 4 > Type esp32 in the search box and install the latest version
Step 5 Now, we are ready to connect the microcontroller but we need to do two things. First selecing the board, Second selecing the port. To select the board you just have to click on Tools > Board > esp32 > XIAO_ESP32C3
Step 6 To select the port, you can start by connecting the microcontroller to you PC by using a USB wire. Then click on Select other board and port
Step 7 Choose the board and the correct port which will automatically be followed by (USB)
Now, you can see that it is connected
Challenge 1¶
For the challenges, I have used another microcontroller which is Kidsbits ESP32. It was easy to connect it to Arduino. I followed the same steps but I have selected other board and port.
Challenge 1 was to blink my led but have my blink delay periods be randomized values between 1 second and 5 seconds. So, for the coding I used ChatGPT to give me the code
So, I copied the code below and paste it in Arduino IDE.
const int ledPin = 13; // ## LED connected to pin 13
void setup() {
pinMode(ledPin, OUTPUT);
randomSeed(analogRead(0)); // ## Initialize random generator
}
void loop() {
digitalWrite(ledPin, HIGH); // ## Turn LED on
delay(random(1000, 5001)); // ## Random delay between 1 and 5 seconds
digitalWrite(ledPin, LOW); // ## Turn LED off
delay(random(1000, 5001)); // ## Random delay between 1 and 5 seconds
}
Challenge 2¶
In this challenge I had to pre code my microcontroller to send a Morse code 1 word message. The word is Black. So, I used ChatGPT to generate the code.
// Morse code for "Black":
// B = -...
// L = .-..
// A = .-
// C = -.-.
// K = -.-
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
sendMorse("-... .-.. .- -.-. -.-");
delay(10000); // Wait 10 seconds before repeating the message
}
void sendMorse(const char *message) {
while (*message) {
if (*message == '.') {
dot();
} else if (*message == '-') {
dash();
} else if (*message == ' ') {
delay(3000); // Space between letters (3 seconds)
}
delay(500); // Gap between dots and dashes (0.5 second)
message++;
}
delay(5000); // Gap at the end of a full word (5 seconds)
}
void dot() {
digitalWrite(ledPin, HIGH);
delay(1000); // Dot duration (1 second)
digitalWrite(ledPin, LOW);
}
void dash() {
digitalWrite(ledPin, HIGH);
delay(2000); // Dash duration (2 seconds)
digitalWrite(ledPin, LOW);
}
Challenge 3¶
The third challenge was to select the duration of the light being on and off while the program is running by entering it in the serial monitor. We were asked to Write a code that takes the data from the serial monitor and delays by that amount of time.
const int ledPin = 13;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
Serial.println("Send 'ON:ms,OFF:ms' (e.g., ON:500,OFF:200)");
}
void loop() {
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
input.trim();
int onTime = input.substring(3, input.indexOf(',')).toInt();
int offTime = input.substring(input.indexOf("OFF:") + 4).toInt();
Serial.print("Blinking: ON=");
Serial.print(onTime);
Serial.print("ms, OFF=");
Serial.println(offTime);
while(1) { // Runs forever until new Serial input
digitalWrite(ledPin, HIGH);
delay(onTime);
digitalWrite(ledPin, LOW);
delay(offTime);
// Check for new input during delays
if (Serial.available()) break;
}
}
}
Now, if I want to run the code I have to upload it and then click on serial monitor
Then, I have to type any value and the led will operate based on that value
MicroBlocks¶
The second programming environment that I have used is called microblocks which is very easy to use and it was designed to make programming easier and more fun. The steps below will explain how to connect the board.
Step1 in the homepage click on Run
Step 2 Click on Setting > Update firmware on board
Step 3 Choose the board which is in my case ESP32
Step 4 Click on Connect > connnect USB
Challenge 1¶
Since we were asked to do the same challenge on two different environment, I did it on microblocks and it was easier, I just had to drag the boxes. So, the outcome was as shown below:
Challenge 2¶
For the second challenge it was also easy but long, so I will show how the blocks like at the beginning.
So I kept adding the same blocks but every time I changed the millisecs.