4. Embedded Programming¶
Group Task (Microcontroller Search)¶
What is a Microcontroller¶
A microcontroller is a compact integrated circuit designed to govern a specific operation in an embedded system, the typical microcontroller includes a processor, memory and input/output (I/O) peripherals on a single chip Source
Difference between a Microcontroller and Microcontroller Board (Single Board Computer)
Is that the former is a self-contained computer with the ability to run an OS, whereas the latter is just a chip with far fewer resources - Soucre
Microcontroller details¶
* Name: Arduino nano 33 iot * Pins number: 22 pins (14 digital I/O pinsand 8 analog input pins)[casual] andcanaddwith PWM pins – but in ours its 30 pins * Pins Type: digital I/O pins, analog input/output pins, PWM Pins (similar to squarelaser pattern) * Sensor: No built in Sensors - Internal Thermal Sensor - can attach IMU sensor in the nano form factor - IMU (LSM6DS3) * Processor: low power Arm® Cortex®-M0 32-bit SAMD21 * Connection: WiFi - Bluetooth - IMU * Power: usually 5V * Programming language: Arduino IDE 2.0 / Arduino IDE 1.8 / Web Editor (Guaranteed) ; C++ , Python [For arduino in common] * Applications: IoT project with the need for compact size - Update current projects based on the Nano * Price: 25 Dollar
Can also be edited with:
Arduino IDE (C++),
MU editor (Python) and
Any Online Blocks program
Microcontroller datasheet¶
from this datasheet, you can see the features, electrical specifications, basic hardware implementation examples, pin definitions and footprint dimensions Source
My Groupmate Ahmed’s Link to his Group Task/Assignment
Individual Task (Programming Microcontroller)¶
Beginning of the Journey (Arduino 2.1.1)¶
In order to start programming the Arduino Nano 33 IoT, I followed the instructions of my supervisor to download Arduino 2.1.1 so that I can start programming, it took from me 7 minutes to download and 7 minutes to install.
Programming Process¶
I started by choosing the following Procedures in Arduino 2.1.1 so that I can begin programming my microcontroller
- Tools > Board > Arduino AVR Boards > Arduino Nano
- Tools > Port > Arduino Nano 33 IoT
- Tools > Processor > ATmega328P (Old Bootloader)
- Select from the rectangle space at the right of the verify and upload button to pick > Arduino NANO 33 IoT (you can download drivers if it does not work after it fails, the system will suggest installing its processor)
Program details (Arduino 2.1.1 [C++])¶
Arduino IDE, Mu, TinkerCAD Are Programming environments for microcontrollers, meaning they are having tools used in the development of software - Source
I Personally Recommend using this programming software as it provides many examples and there are ready codes on online website and using chatGPT can help you get more codes for it, it is very convenient.
Blink (Easy Mode)¶
I can get an example of blink which simplifies my work by choosing from inside Arduino 2.1.1: * File > Examples > 01.Basics > Blink
after choosing this exmaple, i edit it and add the part for getting random values and then add them into the microcontroller.
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
//A
//dot
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(500); // wait for a second
//dash
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
//gap between letters
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000); // wait for a second
//L
//dot
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(500); // wait for a second
//dash
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
//dot
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(500); // wait for a second
//dot
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(500); // wait for a second
//gap between letters
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000); // wait for a second
//I
//dot
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(500); // wait for a second
//dot
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(500); // wait for a second
//gap at end of word
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(5000); // wait for a second
}
Morse Code (Medium Mode)¶
_in order to add a letter pattern, I had to give the program the meaning of each letter which will take a huge amount of time. instead, I inserted into the code the letters I needed for a name only.
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop ()
{
delay(10000);
//A
//dot
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(500); // wait for a second
//dash
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
//gap between letters
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000); // wait for a second
//L
//dot
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(500); // wait for a second
//dash
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
//dot
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(500); // wait for a second
//dot
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(500); // wait for a second
//gap between letters
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(3000); // wait for a second
//I
//dot
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(500); // wait for a second
//dot
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(500); // wait for a second
//gap at end of word
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(5000); // wait for a second
delay(60000);
}
Light Duration (Hard Mode)¶
after uploading the code, you enter a number then the program is luminating for a duration that was set.
/*
Blink
Turns an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino
model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
int dfu=0;
void loop()
{
if(Serial.available() > 0)
{
dfu=Serial.read();
}
if(dfu =='1')
{
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
}
else if(dfu =='2')
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
else if(dfu =='3')
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(3000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
else if(dfu =='4')
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(4000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
}
}
TinkerCAD¶
TinkerCAD can be used to design circuits and give orders to microcontrollers for simulations, similar to Arduino IDE but TinkerCAD does not work on real microcontrollers (as far as I know) and does not seem to provide the features as in Arduino IDE, even though it is still convenient for simulations.
Downloaded TinkerCAD Code¶
Normal Blink¶
After downloading TinkerCAD Code, you get an .ino file that can be opened in Arduino IDE and uploaded at Arduino IDE as C++ Code.
// C++ code
//
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}g
Faster Blink, Longer Delay¶
Blinking for 0.5 Seconds while delaying for 2 Seconds.
// C++ code
//
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Will Wait According to my code for 0.5 seconds after being on
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Will Wait According to my code for 2 seconds after being off
}