4. Embedded programming¶
Week 5¶
According to what I learned at university; a microcontroller is a small integrated circuit that controls a single function in an embedded system. On a single chip, a microcontroller has a CPU, memory, and input/output (I/O) peripherals. Microcontrollers, also known as embedded controllers or microcontroller units (MCUs), are found in a variety of devices, including automobiles, robotics, office equipment, medical devices, mobile wireless transceivers, vending machines, and home appliances. They are basically simple microscopic personal computers (PCs) with no complicated front-end operating system, meant to operate minor characteristics of a bigger component (OS).
How do microcontrollers work?¶
A microcontroller is a device that is integrated into a system and controls a single function. It accomplishes this by employing its core CPU to evaluate data received from its I/O peripherals. The microcontroller’s temporary data is saved in its data memory, where the processor retrieves it and decodes and applies the incoming data using instructions stored in its program memory. It then communicates with its I/O peripherals and takes the required action.
Microcontroller applications¶
• light sensing. • Temperature sensing. • Fire detection. • Industrial instrumentation devices. • Process control devices.
Group assignment¶
The MBED-005.1 microcontroller was given to us with the task of identifying the following characteristics: • Type of microcontroller: Mbed-005.1 • Number of pins: 40 pins. • Type of pins: Analog and PMW output. Analog inputs: The analogue inputs are used in control systems with input sensors that produce a voltage, current or resistance change in response to an environmental variation or system measurement. PWM: is a way to control analog devices with a digital output. Another way to put it is that you can output a modulating signal from a digital device such as an MCU to drive an analog device. • Power source required: 5V USB or 4.5-9V supply, 60-120mA (Vin). • Type of Sensors: Temperature, Distance, Light, Acceleration, GPS coordinates and heart rate. • Languages used to program the microcontroller: C & C++(using Arduino IDE software). Datasheet we used Here Group Assignment
Individual assignment¶
This week, we were given the challenge of using the Arduino IDE program to connect an LED to the Adafruit (Bluefruit Sence). LED: When an electric current runs through a Light Emitting Diode (LED), it emits light. Light is produced when holes from p-type semiconductors recombine with electrons from n-type semiconductors. The wavelength of the light emitted is determined by the semiconductor material’s bandgap. Wider bandgaps are found in harder materials with stronger molecular connections. Ultra-wide bandgap semiconductors are aluminum nitride semiconductors.
Because I already had the software, all I had to do was change the board from an Arduino UNO to an Adafruit feather nRF52832. If you get Adafruit feather sense this link will help you. Adafruit feather sense
Coding¶
void setup() {
pinMode(LED_BUILTIN , OUTPUT);
} // identify digital pin LED_BUILTIN as an output.
void loop() {
digitalWrite(LED_BUILTIN , HIGH); // Turn the LED ON
delay(5000); // wait for 5 sec
digitalWrite(LED_BUILTIN , LOW); // Turn the LED OFF
delay(1000);// wait for a second
}
Code explanation:¶
Void setup and Void loop
Voide setup: The code you enter within void setup () will only be executed once, and that will be at the start of your application.
Void loop: This is where the bulk of your Arduino program runs. The program begins immediately after the opening curly bracket, continues until it encounters the closing curly bracket, then jumps back to the first line in loop and repeats the process (The loop function runs forever). pinMode(): Configures the specified pin to behave either as an input or an output.
digitalWrite(): To a digital pin, write a HIGH or LOW value. If pinMode() has been used to establish the pin as an OUTPUT, the voltage will be set to the appropriate value: 5V (or 3.3V for 3.3V boards) for HIGH(ON), 0V (ground) for LOW(OFF). If the pin is set to INPUT, digitalWrite() will activate (HIGH) or disable (LOW) the input pin’s internal pullup. The internal pull-up resistor should be enabled by setting pinMode() to INPUT PULLUP.
Delay(): This number represents the time (measured in milliseconds). The program should wait until moving on to the next line of code when it encounters this function.
TinkerCAD:¶
TinkerCAD is a great tool for beginners who want to learn how to program microcontrollers. Steps: 1. Open TinkerCAD circuitry. 2. Slide the Arduino Uno R3 into position. 3. Then, in the upper right corner, click on code. 4. The pre-made code will display, and you may begin stimulation right away. 5. You may convert the code to text for usage in other software by changing the blocks on the left to text. Next, Morsecode was introduced to us, which we will apply to create circuits in TinkerCAD. High= - Low=. I chaos the word (instrumentation) and translate it to Morse code using this website (.) =0.5 sec (HIGH);
(-) = 1 sec (HIGH)
Gap between dash & dot= 0.5 sec (LOW)
Gap between letter= 2 sec (LOW)
After that, I re-coded my morse code into TinkerCAD. For several of the scripts that were in repeat, I applied the repeat option. After designing the code in TinkerCAD, I downloaded it into Arduino IDE software to upload it to my microcontroller (Adafruit), here is the code:
// C++ code
//
int counter;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
for (counter = 0; counter < 2; ++counter) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
}
}
void loop()
{
delay(10); // Delay a little bit to improve simulation performance
}
Challenges¶
Easy Mode¶
Blink your led but have your blink delay periods be randomized values between 1 second and 5 seconds.
// C++ code
//
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(1000 * random(1, 5 + 1)); // Wait for 1000 * random(1, 5 + 1) millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(1000 * random(1, 5 + 1)); // Wait for 1000 * random(1, 5 + 1) millisecond(s)
}
Medium mode¶
pre code your microcontroller to send a Morse code 1 word message and challenge a friend, family member, your colleagues or your instructor to figure it out. dot = 0.5 second light on. dash=1 second light on. gap between dots and dashes=0.5 second light off. gap between letters=2 second light off I chaos the word (Coding) and translate it to Morse code using this website (.) =0.5 sec (HIGH);
(-) = 1 sec (HIGH)
Gap between dash & dot= 0.5 sec (LOW)
Gap between letter= 2 sec (LOW)
After that, I re-coded my morse code into TinkerCAD. For several of the scripts that were in repeat, I applied the repeat option. After designing the code in TinkerCAD, I downloaded it into Arduino IDE software to upload it to my microcontroller (Adafruit), here is the code:
// C++ code
//
int counter;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(0, OUTPUT);
for (counter = 0; counter < 2; ++counter) {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(0, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(2000); // Wait for 2000 millisecond(s)
}
}
void loop()
{
delay(10); // Delay a little bit to improve simulation performance
}
How do I feel bout this week?¶
I’ve been waiting for this week from the beginning, since it relates to my major, it was very interesting, and I learned ne things. For instance, at university, we use the Arduino UNO type, but here I used a new type called the Adafruit Sense, and I ran into few problems, but overall, it was the best week for me! I’m looking forward to enjoying the Input and Output Devices.