Skip to content

4. Embedded programming

This week I worked on embedded programming on an Arduino microcontroller that gets coded and given commands using different programming languages. I worked on two languages which are Arduino IDE and Python.

What’s microcontroller

“A microcontroller is a compact integrated circuit designed to govern a specific operation in an embedded system. A typical microcontroller includes a processor, memory and input/output (I/O) peripherals on a single chip.”

Group assignment

In the beginning, we learned what a microcontroller is. After that, we were directed to find and assess several microcontroller board types.

individual assignment

We were asked to program a microcontroller chip. To do this, there are many languages a programmer can use (Python, C++, and others..)

First, we started by downloading and installing the Arduino IDE and MU editor. Default settings for both installation and download procedures were followed. And then we had some challenges to perform using these two programs.

Tasks

Using Arduino IDE

The first task was blinking, which required turning the LED on for periods of time and then turning it off for other periods.

First, we used ready code from the basic codes in the Arduino IDE. Then in order to observe a difference, we changed the periods between the light on and off.

  • Code:
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
}

Using MU Editor

Then, using Mu Editor I Programmed the microcontroller using Python language, in order to make it flash between fixed intervals, and then made it flash between random intervals where first I made it flash between random intervals between 1 to 5, but I didn’t notice the interval difference clearly, then I put it between 1 to 10, so the difference was clear between periods.

  • Code | for random intervals:
import time
import board
import digitalio
from random import randint

led = digitalio.DigitalInOut(board.D6)
led.direction = digitalio.Direction.OUTPUT


while True:
    t= randint(1,6)
    led.value = True
    time.sleep(t)
    led.value = False
    time.sleep(t)

Vedio

 

Fade

Using Arduino IDE

I programmed the microcontrollers to make the light fade in and off, using the ready basic codes from Arduino EDI (File >> Examples >> basic >> Fade), and modifying them to suit the microcontroller used for the pin number, face amount, and delay.

int led = LED_BUILTIN;         // the PWM pin the LED is attached to
int brightness = 0;  // how bright the LED is
int fadeAmount = 20;  // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 10 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(60);
}

   

Morse code

What is morse code?

Morse code is a method used in telecommunication to encode text characters as standardized sequences of two different signal durations, called dots and dashes, or dits and dahs. Morse code is named after Samuel Morse, one of the inventors of the telegraph.

Basic For the medium mode challenge, a Morse code using Mu Editor will be constructed to show any word and I choose my name “BATOOL”.

Python file: morse - typed in file

Morse - input from user

Then using some ready-made code from geeksforgeeks.org for morse translator, I edited and added to it to make whatever text was written display as morse code in the microcontroller.

Python file Morse - input from user file

Vedio

 

Time depend on user input

Just as we programmed the microcontroller to turn on and off at a specific or random time, we have programmed it to turn off and on at a time that depends on the user’s input. I have programmed it using Python at Mu Editor.

Vedio

 

Code

import time
import board
import digitalio

led = digitalio.DigitalInOut(board.D6)
led.direction = digitalio.Direction.OUTPUT

durationl = int(input("Enter duration to light"))
durations = int(input("Enter duration to sleep"))

while True:
    led.value = True
    time.sleep(durationl)
    led.value = False
    time.sleep(durations)


Last update: June 10, 2023