4. Embedded programming¶
This week we will learn about micro controllers, what are micro controllers , how do they work , why are we learning about them.
What are micro controllers¶
Micro controllers are considered as a small computer, they are made out of a CPU, RAM , MEMORY , I/O ports, they are cheap computers that can be used to create independent systems that do not need human interference by programming them to receive a specific input to give a required output.
Learn to search¶
We started this week by getting deferent types of Micro controllers, and to search about their properties and their features like for example :
TEENSY 3.2 which is a small MC, you can find the module name on the back of the device, by searching on the internet we found the following information :
So , we should always search about the MC that we will use to know if it is suitable for the type of project that we are working on.
How to use ?¶
We can start by downloading Arduino IDE
After downloading IDE this will be the first thing we see, we will notice that we have the code divided in two parts, void setup and void loop, the setup part is to tell the MC the names od the pins you will be using , like for example
pinMODE(PIN, STATUE); where you will give a PIN a refrence to the device so it know that it is the one used and it will be input for example, and for the void loop you can use (digital/analog)(read/write) to receive the required input or give the required output for the machines, so basically we understand that MC are machines that are told about the pins used to get data written as an output or read data from sensors .
Adafruit FEATHER 52840 sense
This is the MC that we are going to be using for writing our code using ARDUINO IDE, you can find the MC description in the table provided in this up here in this page HERE.
This is how Ardafruit FEATHER 5240 SENSE look like :
You will need to reset the board after connecting it to your computer, using the button near the RGB LED with fast two clicks when it is ready you will see the RGB turn green indicating that it is ready.
Running IDE to start coding, first thing we will do (if the board library does not exist) is to download it by adding the URL following the steps provided :
If we cant find the board :
Searching the internet for a Library URL to download it in IDE (Searching for adafruit feather bluefruit sense installation GUIDE ) we found the URL HERE
Putting the link here :
Downloading the library :
CODING AND APPLICATIONS :¶
Arduino IDE:¶
We started by connecting the board and using the available examples for previous codes.
making the LED on the board turn on for a specific time and sequence or by using this code :
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(500);}
Running the code using the yellow arrow,or check if it is correct clicking on the icon on the left of the arrow
If you wanted to make the LED time to be random just change the number in delay(X) to delay(random)
The simple examples are available to access but i will show one off my tasks in doing moors code to blink SOS using the built in LED, i made the RGB LED on the MC work in red and blue after the moors code.
The SOS code using IDE
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// Execute the Morse code loop
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(3000);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(3000);
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(3000)
}
The adjusted code to show emergency light :
#include <Adafruit_NeoPixel.h>
#define LED_PIN 8
#define NUM_PIXELS 2
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pixels.begin();
digitalWrite(LED_BUILTIN, HIGH);
pixels.begin();
// Turn on the built-in red LED
}
void loop() {
// Execute the Morse code loop
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(3000);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(3000);
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(3000);
for (int i = 0; i < 8; i++) {
pixels.setPixelColor(0, pixels.Color(255, 0, 0)); // Red
pixels.setPixelColor(1, pixels.Color(0, 255, 0)); // Green
pixels.show();
delay(250);
pixels.clear();
pixels.show();
delay(250);
}
// Turn off RGB LED
pixels.clear();
pixels.show();
delay(3000);
}
The final result:
Micro python using Thonny¶
Thonny is a programming tool that alow you to connect MC’s and program it using micro python, which is a very basic and simple form of python that the MC understand.
Starting by connecting the MC to the computer and then following the steps so you can code on the MC using Thonny.
Starting by defining the libraries, like pins, to allow you to control them and also time.
Starting by the simplest code to turn on a LED “After” defining pins and time :
from machine import Pin
import time
red_pin = Pin(13, Pin.OUT) # GPIO pin for the red color
yellow_pin = Pin(14, Pin.OUT)
green_pin = Pin(15, Pin.OUT) # GPIO pin for the green color
red_pin.on()
yellow_pin.off()
green_pin.off()
time.sleep(5)
To not git in to much boring details we understood that we can control pins to get a specific output, for the remaining i will show my work using micro python showing that it is a very easy and entertaining method that even kids can learn.
Examples :¶
Police lights using RGB-LED:¶
from machine import Pin
import time
# Define the pins for the RGB LED and the fan
red_pin = Pin(13, Pin.OUT) # GPIO pin for the red color
green_pin = Pin(14, Pin.OUT) # GPIO pin for the green color
blue_pin = Pin(15, Pin.OUT) # GPIO pin for the blue color
button_pin = Pin(20, Pin.IN)
# Function to control the RGB LED colors
def set_colors(red_state, green_state, blue_state):
red_pin.value(red_state)
green_pin.value(green_state)
blue_pin.value(blue_state)
# Initial LED state
set_colors(0, 0, 0) # All LEDs off initially
# Loop to change colors when the button is pressed
while True:
if button_pin.value() == 0: # Button is pressed
for _ in range(8): # Iterate over the sequence
set_colors(1, 0, 0) # Red
time.sleep_ms(350)
set_colors(0, 0, 1) # Blue
time.sleep_ms(350)
time.sleep(0.1) # Small delay to avoid rapid checking of the button
The result:
Changing RGB-LED color¶
from machine import Pin
import time
# Define the pins for the RGB LED and the button
red_pin = Pin(13, Pin.OUT) # GPIO pin for the red color
green_pin = Pin(14, Pin.OUT) # GPIO pin for the green color
blue_pin = Pin(15, Pin.OUT) # GPIO pin for the blue color
button_pin = Pin(20, Pin.IN) # GPIO pin for the button
# RGB color states
OFF = (0, 0, 0)
RED = (1, 0, 0)
GREEN = (0, 1, 0)
BLUE = (0, 0, 1)
YELLOW = (1, 1, 0)
CYAN = (0, 1, 1)
MAGENTA = (1, 0, 1)
WHITE = (1, 1, 1)
colors = [OFF, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE]
current_color = 0
button_press_count = 0
# Function to set the RGB LED color
def set_color(color):
red_pin.value(color[0])
green_pin.value(color[1])
blue_pin.value(color[2])
while True:
if button_pin.value() == 0: # Button press detected
button_press_count += 1
current_color += 1
if current_color >= len(colors):
current_color = 0
set_color(colors[current_color])
time.sleep(0.5) # Delay to debounce the button press
if button_press_count == 8:
set_color(OFF)
button_press_count = 0
time.sleep(0.1) # Small delay to avoid rapid checking of the button
The result :