Skip to content

4. Embedded programming

This week we worked on embedded programming. We worked with the Arduino Nano 33 BLE sense. We learned about it, and we used it to do some simple functions.

Arduino Nano 33 BLE sense

First of all, the device was introduced to us. We learned what a microcontroller is and what it is used for. We learned about the Arduino Nano, and how it differs from some other Arduinos.

This is the Arduino’s general structure and pinout, it contains many pins including several analog and digital pins.

Specifications

This Arduino has a very small size compared to many other Arduinos. Moreover, it contains an improved processor over the Arduino Nano, and it contains a multitude of sensors, which can make it ideal for use in many projects.

It has :

1- 9-Axis inertial sensor

2- Humidity and temperature sensor

3- Barometric sensor

4- A digital microphone sensor

5- Gesture, proximity, light color, and light intensity sensor

Setting it up

After that, each of us was given a device to use. We went on and read about setting it up on this site NANO 33 BLE Sense Setup.

I followed the steps and downloaded the Arduino IDE, which is used as a programming environment for Arduino devices, and is used to upload programs to the Arduino.

Then I had to install the Arduino Mbed OS Nano Boards package by going to the tools menu then Boards then Boards manager.

After that, from the same tools menu, we choose Boards and then choose our board, which appears as Arduino Nano 33 BLE.

then we need to choose the port, so that the software knows from where it should upload the programs to the Arduino as shown in the next picture

Now that the Arduino was set up, the next step was testing it. A great thing about the Arduino IDE is that it includes many example programs, we chose the Blink program to test on our device for its simplicity.

We then uploaded the program to the device through the USB cable, and the built-in LED started blinking. After that, we edited the example code and changed the on and off periods for the LED, which was successful.

Challenges

After getting to know the Arduino, we were given some challenges to take on. There were 3 levels

Easy : Make the LED blink on and off for random periods

Intermediate : Make the LED blink Morse code

Hard : You select the duration of the light being on and off while the program is running by entering it in the serial monitor. The code should take data from the serial monitor and delay by that amount of time.

Easy

I started with the easy challenge. I knew I had to find how to generate random numbers in the Arduino IDE. Thankfully, I found my answer on this website. This is the code used

void setup() {
  // put your setup code here, to run once:

}
 long randNum;
void loop() {
  // put your main code here, to run repeatedly:

  randNum=random(1000,5000);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(randNum);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);

}

This is my Arduino blinking the code

Intermediate

I then started to work on the intermediate challenge. I was not familiar with Morse code, so I had to look it up.

I then wrote a small program to blink some Morse code. The parameters were
one second on is dash, half a second is dot, one second between dots and dashes, and two seconds between letters. I didn’t know what to make it blink, so I decided why not the word morse. Copying and pasting sections of the code made the coding process easier, all what was left is to change the periods, also keeping some empty lines between letters in the code helps to organize things.

This is me looking up Morse codes

and this is the code I wrote

void setup() {
  // put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(1000);                       
  digitalWrite(LED_BUILTIN, LOW);    
  delay(500);

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


  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(2000);


  digitalWrite(LED_BUILTIN, HIGH);   
  delay(500);                       
  digitalWrite(LED_BUILTIN, LOW);    
  delay(500);

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

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


  digitalWrite(LED_BUILTIN, HIGH);   
  delay(500);                       
  digitalWrite(LED_BUILTIN, LOW);    
  delay(500);

  digitalWrite(LED_BUILTIN, HIGH);   
  delay(500);                       
  digitalWrite(LED_BUILTIN, LOW);    
  delay(500);

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


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

}

This is my Arduino blinking the code

Tinker CAD and Further Morse

Tinker CAD

Our next goal was to learn another method to program the Arduino. We used the website Tinker CAD, which can be used to design electronic circuits. I created a new design and included the Arduino Uno which can be found in components. This is the website interface

After adding the component, you can edit the code in the code tab. You can also press start simulation to test your circuit as if it is a real circuit. When opening the code tab, you will notice the code for blinking is there by default. I pressed start simulation and the LED started blinking. Then I changed the blinking on and off durations and tested it again.

After that, I downloaded the code through the download button, which is an arrow going down. The code gets downloaded to a file which can opened with the Arduino IDE. However, beware as the file needs to be in a folder before it is possible to open, if you try to open it before putting it in a folder, then it will give you a warning and it can put itself in a folder.

I then connected my Arduino and uploaded the program to it to test it there which was successful.

We then tried to do more Morse code, but this time using Tinker CAD. We first used the website https://morsecode.world/international/translator.html to translate a message into Morse code. Then, I kept trying to make the dots and dashes by duplicating the original blinking block and changing the wait duration so it can be either a dot, a dash, a break between letters, or a break between letters.

However, This turned out to be very tedious and messy. In the end, things got mixed up and the attempt was not successful.

Better techniques

I then went back to the Arduino IDE and started to try a new thing, which could make writing Morse much easier; that is using functions.

This can be implemented in two ways:

1-Coding the main Morse characters, such as dot, dash, break between letters, and the break between words, as functions and calling these functions whenever you want them. This way, each function contains the necessary code with the appropriate on and off durations.

2-Using the first method and then creating more functions for every letter of the alphabet and every character with the functions from method 1 . This is longer, but makes writing in Morse later as easy as typing on a keyboard.

This is me trying to learn how to code functions.

This is the code I wrote for the phrase “hi world”

    void setup() {
      // put your setup code here, to run once:
      pinMode(LED_BUILTIN, OUTPUT);
    }

    int j= 250;
    int i= 2*j;
    void d(){digitalWrite(LED_BUILTIN, HIGH);   //dash
      delay(i);                       
      digitalWrite(LED_BUILTIN, LOW);    
      delay(j);
    }
    void dt(){digitalWrite(LED_BUILTIN, HIGH);   //dot
      delay(j);                       
      digitalWrite(LED_BUILTIN, LOW);    
      delay(j);
    }
    void b(){                 //break between letters
      digitalWrite(LED_BUILTIN, LOW);    
      delay(2*j);
    }
    void s(){                 
      digitalWrite(LED_BUILTIN, LOW);    //space between words
      delay(2*j);
    }
    void loop() {
      // put your main code here, to run repeatedly:
      dt();
      dt();          //h
      dt();
      dt();

      b();

      dt();
      dt();         //i

      b();

      s();

      dt();     
      d();          //w
      d();

      b();

      d();
      d();          //o
      d();

      b();

      dt();
      d();          //r
      dt();

      b();

      dt();
      d();          //L
      dt();
      dt();

      b();

      d();
      dt();         //d
      dt();

      b();      
    }

and this is where I got my phrase translated

Writing the code this way was faster, and it made it much easier to be organized, especially by adding empty lines to separate characters.

This is my Arduino blinking the code


Last update: November 15, 2021