Skip to content

4. Embedded programming

During our group assignment, we all went through several micro-controller boards. Group Assignment - Embedded programming

Board Info

Here is the manufacturer spec list of the MKR1010 board I will be using.

Microcontroller SAMD21 Cortex®-M0+ 32bit low power ARM® MCU (datasheet)
Radio module u-blox NINA-W102 (datasheet)
Board Power Supply (USB/VIN) 5V
Secure Element ATECC508 (datasheet)
Supported Battery Li-Po Single Cell, 3.7V, 1024mAh Minimum
Circuit Operating Voltage 3.3V
Digital I/O Pins 8
PWM Pins 13 (0 .. 8, 10, 12, 18 / A3, 19 / A4)
UART 1
SPI 1
I2C 1
Analog Input Pins 7 (ADC 8/10/12 bit)
Analog Output Pins 1 (DAC 10 bit)
External Interrupts 10 (0, 1, 4, 5, 6, 7, 8,9, 16 / A1, 17 / A2)
DC Current per I/O Pin 7 mA
CPU Flash Memory 256 KB (internal)
SRAM 32 KB
EEPROM no
Clock Speed 32.768 kHz (RTC), 48 MHz
LED_BUILTIN 6
USB Full-Speed USB Device and embedded Host
Length 61.5 mm
Width 25 mm
Weight 32 gr.

Mu (Circuit-Python Programming)

First I started with Python. To get this to work we need to do a few things:- 1- Download Mu Python 2- Download CircuitPython for your specific board 4- Download the latest Library Bundle from CitcuitPython to extend functionality (optional)

To get CircuitPython on our board, we simply need to do the following: 1- Double tap the reset button on the MKR 1010 2- Click and drag the M2U file on the board 3- The device should disconnect and reconnect with CircuitPython ready to go

Last step, open the code.py Mu (file can be found on your MKR 1010 drive). We can simply write our code, and save it, it will then instantly update on the MKR 1010.

I am somewhat familiar with coding but it’s my first time using python, so instead of just simply blinking the LED manually, I wrote a code that takes a morse code text and blinks it automatically.

import board
import digitalio
import time

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


def blinkLED(code):
    duration = 0 #Reset Blink Duration
    space = 0.5 #Reset Space Duration
    if code == ".": #if Dot
        duration = 1
    elif code == "-": #if Dash
        duration = 2
    elif code == " ": #if space, it will count it as a new letter gap
        space = 3
        time.sleep(space)
    if code == "." or code == "-": #if its a morse code, then blink it
        led.value = True
        time.sleep(duration)
        led.value = False
        time.sleep(space)

while True:
    for x in "... --- ... ---": #Example Morse Code Input. It will break the string into characters
        blinkLED(x)
        print(x)

Arduino IDE (C Programming)

Next we will use the Arduino IDE and write a whole bunch of test code on it. To get started: 1- Download the Arduino IDE 2- Connect the board (you may be prompted to download additional files, which might be required so everything can run)

Wiring diagram (I used the Uno R3 just for the diagram) Here is the first example, my comments are found in-line.

  int button=0; //Button
  int delayTime=1000;

  void setup()
    {
    pinMode(LED_BUILTIN, OUTPUT); //Set the Builtin LED as an Output
    pinMode(7,INPUT_PULLUP); //Usually you need to use a resistor to make the button not trigger uncontrollably. However you can also use PullUp feature instead
    }

void loop() {

  button=digitalRead(7); //Read the button status, 1 is open, 0 is pressed

  if(button==0) //check if the button is pressed
  {
    delayTime=random(1000,5001); //Generate a random number between 1000 and 5000
    Serial.println("Button press detected");
  }

  else //if the button is not pressed, then continue to use the delayTime generated
  {
    digitalWrite(LED_BUILTIN, HIGH); //Turn on the LED
    delay(delayTime); //Set Delay
    digitalWrite(LED_BUILTIN, LOW); //Turn off the LED
    delay(delayTime); //Set Delay
  }

}

LED as Photodiode

All LEDs can actually also act as photodiodes, and the same is true of speakers acting as microphones. We can test this by using the LED as an input rather than an output.

int delayTime=1000; //1 second delay
int light=0; //initialize the light value

void setup()
  {
  pinMode(LED_BUILTIN, INPUT); //set the builtin LED as an input (photodiode)
  }

void loop()
  {
  light=analogRead(LED_BUILTIN); //read the value at the LED
  delay(delayTime);
  Serial.println(light);
  }

Morsecode on button press

int button=0;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(5,INPUT_PULLUP);
}

void loop()
{
  button=digitalRead(5);

  if(button==0)
  {  
  dotPulse(); //Made the dot and dash functions to simplify the code
  dotPulse();
  dotPulse();
  delay(3000);
  dashPulse();
  dashPulse();
  dashPulse();
  delay(3000);
  dotPulse();
  dotPulse();
  dotPulse();
  delay(3000);
  }
}

void dotPulse()
{
  digitalWrite(LED_BUILTIN, HIGH); //Turn on the LED
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW); //Turn off the LED
  delay(500);
}

void dashPulse()
{
  digitalWrite(LED_BUILTIN, HIGH); //Turn on the LED
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW); //Turn off the LED
  delay(500);
}
int delayTime=1000; //initialize the delay timer

void setup()
{
  pinMode(4, OUTPUT);
}

void loop()
{
  delayTime = Serial.parseInt(); //take the user input from the serial monitor and convert it into a number
  digitalWrite(4, HIGH);
  delay(delayTime);
  digitalWrite(4, LOW);
  delay(delayTime);
}

Full Word to Morse Code app (with visualizer in the Serial Monitor)

I wrote this final app to make it work with any word (alphabet letters only, but it can easily be extended further). This code doesn’t catch errors..etc as it’s just a quick fun code I wrote.

Wiring Diagram

String keyWord=""; //Variable to store the current keyword entered
int buzzer=6; //setting the buzzer pin
int longBlink=400; //all these values can be easily customized
int shortBlink=50;
int letterDelay=350; //if the delay is too fast the buzz is not heard clearly

char letters[]= {'a',   'b',    'c',    'd',    'e',    'f',    'g',    'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',    'p',    'q',    'r',    's',    't',    'u',    'v',    'w',    'x',    'y',    'z'}; //This is so we can get the index number of the letter

//This is the Alphabet morse code, it can easily be extended further
char morseCode[26][4] = { 
  {'1','2','0','0'}, //A
  {'2','1','1','1'}, //B
  {'2','1','2','1'}, //C
  {'2','1','1','0'}, //D
  {'1','0','0','0'}, //E
  {'1','1','2','1'}, //F
  {'2','2','1','0'}, //G
  {'1','1','1','1'}, //H
  {'1','1','0','0'}, //I
  {'1','2','2','2'}, //J
  {'2','1','2','0'}, //K
  {'1','2','1','1'}, //L
  {'2','2','0','0'}, //M
  {'2','1','0','0'}, //N
  {'2','2','2','0'}, //O
  {'1','2','2','1'}, //P
  {'2','2','1','2'}, //Q
  {'1','2','1','0'}, //R
  {'1','1','1','0'}, //S
  {'2','0','0','0'}, //T
  {'1','1','2','0'}, //U
  {'1','1','1','2'}, //V
  {'1','2','2','0'}, //W
  {'2','1','1','2'}, //X
  {'2','1','2','2'}, //Y
  {'2','2','1','1'}, //Z
};


void setup()
{
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(buzzer,OUTPUT); //buzzer pin set to output
}

void loop()
{

  while (Serial.available() == 0) //Wait for a user input
  {
  }
  keyWord=Serial.readString(); //Parse the value as a string
  MorseCodeMatch(keyWord); //send to our function
}

void MorseCodeMatch(String keyWord)
{ 
  keyWord.toLowerCase(); //to avoid any issues with case sensitivity
  int wordLen=keyWord.length()+1;
  char keyChar[wordLen]; //create a character type value array, of the length of the word we are processing

  keyWord.toCharArray(keyChar,wordLen); //Break the string into characters

  for (int i=0; i<wordLen;i++) //go through each letter of the kerWord
    for (int j=0; j<sizeof(letters);j++) //go through the alphabet to find that letter.
       if(keyChar[i]==letters[j]) //trying to match it
          blinkCodeLED(morseCode[j]); //send the letter to be blinked

Serial.println("morseCode Function Done");
}

void blinkCodeLED(char morseMessage[])
{

for (int i=0;i<4;i++) //Length of the Morse code per letter is 4, so loop 4 times
  {
    if(morseMessage[i]=='1') //if Dot
      {
        digitalWrite(LED_BUILTIN, HIGH);
        tone(buzzer, 5000, shortBlink);
        Serial.print("."); //Dot
      }
    else if(morseMessage[i]=='2') //if Dash
    {
      digitalWrite(LED_BUILTIN, HIGH);
      tone(buzzer, 5000, longBlink);
      Serial.print("-"); //Dash
    }
    digitalWrite(LED_BUILTIN, LOW); //Reset LED and put a delay between letters
    delay(letterDelay);}
    Serial.print(" "); //Space
    delay(500);
} 

Last update: June 28, 2024