Skip to content

Week 4: Embedded programming

This week we focused on programming.

  1. We learned about micro-controllers.

  2. We learned about Arduino IDE.

  3. We learned about Thonny.

  4. Arduino UNO


Micro-controllers

in general, their types and capabilities. Each few students were given one micro-controller to to do a quick research about and discover its capacities and limitations (Me and my classmate Sara received an Arduino UNO classic). All the student were asked to write all the their finsings in a share document. You can find in my other classmate’s website Faisal Fathi


Arduino IDE

  1. Go to file then preferences to customize the settings to your preference.

  2. Tic the options compile and Upload. Those options will allow you to see the process of compiling and uploading. This allows you to know what the exact issues if any happens.

  1. As you can see, my micro-controller is Adafruit Feather Bluefruit sense nRF52840.

  2. I connected the micro-controller to my laptop.

  1. Unfortunately, the Adafruit micro-controllers is not available in the Arduino IDE library. Therefore, I had to download it in the app from an External source.

  2. Again, go to File > Preferences and in the box below, add the copied link then click OK. Now you will have to wait until the it finishes downloading.

Note that if your Micro-controller is avaliable in the Arduino IDE liberary you do not have to download anything. Hence, ignore this step.

  1. Go tho the second icon from the vertical bar on the left which is Board Manager then type your micro-controller name - in my case: Adafruit Feather – then download the the package.

  2. In the top left corner, there is a place where you can choose the board as you can see in the photo above. Click on it, then choose your micro-controller and the port. (you can know which port your micro-controller is attached to by removing it from your laptop and then re-attaching it to see which ine of them disappear and then reappear.)

Note that, if you have an adafruit Feather, you have to click the resit button twice (double click) to the computer to identifies it and to mke it appear as port.

  1. Go to File > Examples > Basics > Blink this will open a simple blinking code that allows the built in light to blink.

  2. This is conditions that the code uses. Note that the unit in milliseconds (1000 millisecond = 1 seconds)

Click to expand
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
}
  1. I adjusted the code made the light turn on for 5 seconds and turn off for 0.5 seconds.
Click to expand
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(5000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

Morse code

Click to expand
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(2000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);                      
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(2000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(3000);

  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);   
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(2000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(3000);

  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);                      
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(2000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(3000);

  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(2000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(3000);

  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(3000);

  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(2000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  
  delay(1000);                      
  digitalWrite(LED_BUILTIN, LOW);   
  delay(10000); 
  }

Play the embed tinkerCad below, and try to guess the word that is flicking in morse code from the built-in light of the Arduino.

Guess the Word Game

Guess the Word Game

Can you guess the word? (Hint: POPULAR MASkED TEAM)


Thonny

This IoT kids micro-controllers, it is a simplified version that allows you to connect sensors, motors, or anything easily without a breadboard or external power source.

  1. Open Thonny software then go to tools > Options.

  2. go to Interpreter then choose MicroPython (ESP 32) as a interpreter for running codes. Then, choose the port you are plugging the micro-controller to. Lastly, click on Install or update MicroPython

  1. Again select the target port. Then, select the ESP 32 as a MicroPython family. Next, choose the Expressif*ESP32/WROOM as a variant and the version will be selected automatically. Lastly, click on Install.

  2. Wait until it finishes installation, if it finishes you will see the word done on the left bottom corner. if it appeared, click on close.

  1. After installing the and connecting the Micro-controller, you have to verify that the installation was successful. Therefore, go to View > Files to open the files on a bar on the left side.

  2. if you see the boot.py that means that the installation and connection was successful.

  3. Now go to File > New to open new page to start coding.

  1. This is a simple blinking code that allows the light to turn on for 2 seconds then urn off.

  2. This after adjusting the code to make not to turn off.

  1. Here used the While True command to make the code in a loop while assigning specific values.

  2. Here I am continued using the while True function However, I let the time values be assigned in a random manner with specific range.

Here I plugged an RGB light, and tried to figure out which pin corresponds to which color.

  1. Here is the code for the red light.
Click to expand
from machine import Pin
import time

LEDgreen = Pin(18,Pin.OUT)

LEDred = Pin(17,Pin.OUT)

LEDblue = Pin(19,Pin.OUT)

while True:
    LEDred.value(1)
    time.sleep(5)
    LEDred.value(0)
    time.sleep(5)

  1. Here s the code for the green light.
Click to expand
from machine import Pin
import time

LEDgreen = Pin(18,Pin.OUT)

LEDred = Pin(17,Pin.OUT)

LEDblue = Pin(19,Pin.OUT)

while True:
    LEDgreen.value(1)
    time.sleep(5)
    LEDgreen.value(0)
    time.sleep(5)

  1. Here is the code for the blue light.
Click to expand
from machine import Pin
import time

LEDgreen = Pin(18,Pin.OUT)

LEDred = Pin(17,Pin.OUT)

LEDblue = Pin(19,Pin.OUT)

while True:
    LEDblue.value(1)
    time.sleep(5)
    LEDblue.value(0)
    time.sleep(5)

Arduino UNO

Since this week’s topic was the most interesting, I jumped ahead and used my Arduino kit to try different inputs and output modules. I bought this Arduino kit prior to joining Fablab from Amazon; to try and learn electronics basics on my own in preparation for my final year project in college. However, I got this incredible opportunity to come and learn here at Fablab.

Going through the website of the company that manufactured this kit, I found Tutorial that can be downloaded to guide you through how those components operate.


Stepper motor

The first thing I tried was the Stepper Motor.

After downloading the tutorial files, open it.

  1. Open the “English” folder.

  2. ⁠Go to “codes.

  1. Select the code you want to apply. (in this case, I’m doing the Stepped motor with a remote)

  2. ⁠open the file “with remote”

  3. ⁠Open the code in Arduino IDE

  1. After opening the code in Arduino IDE, we have to include its library now. So go to “sketch” > “include library” > “Add zip library”

  2. ⁠Select the “Stepper” library.

After adding the library, “library included” should be shown in the output.

Now we finished wth the programming aspect let’s move to the electronic aspect.

  1. Go to the previously downloaded tutorial folder again. Then, open the “English” folder > “Elegoo Super Starter Kit for UNO”
  2. ⁠ You will find all the lessons, find the lesson you’re doing and you will see detailed instructions for the electronic connection with a diagram. Follow them carefully.

Here is the final result:

As you can notice if the “VOL +” button bushed, the motor will rotates in clockwise direction. In contrast, if the “VOL -” button bushed, the motor will rotates in anti-clockwise direction.

click here to scroll up


Last update: September 14, 2024