Skip to content

4. Embedded programming

This week I worked on many things:
I worked in defining my final project and I’ve learned about the micro controllers also I’ve tried different software Applications ( Arduino Software ) (Thonny) that provides an easy to use platform for writing,compiling, and uploading code. I will show you the steps of downloading, usage, for each of them and also the experiments that I have done.

Micro controller

The definition:

Micro controllers are integrated circuits (ICs) that consist of a microprocessor core, memory (RAM, ROM, and/or flash), and various peripherals all integrated into a single package. They are designed to execute specific tasks within embedded systems, where they can be found in a wide range of applications including consumer electronics, automotive systems, industrial automation, medical devices, and more.

The features:

  • Integrated Design: They combine a microprocessor core, memory (RAM, ROM, and/or flash), and various peripherals (such as timers, ADCs, UARTs) on a single chip, reducing the need for external components.

  • Low Power Consumption: Designed to operate efficiently on minimal power, making them suitable for battery-powered applications and environments where power efficiency is critical.

  • Real-time Operation: Capable of executing tasks with precise timing requirements, crucial for applications that require immediate responses or time-sensitive operations.

  • Peripherals and Interfaces: Offer a range of built-in peripherals and interfaces (e.g., GPIO, SPI, I2C) that simplify interfacing with sensors, actuators, and other external devices.

Using the Arduino Software (IDE)

  • The Arduino Software (IDE) makes it easy to write code and upload it to the board offline. We recommend it for users with poor or no internet connection. This software can be used with any micro controller board. There are currently two versions of the Arduino IDE, one is the IDE 1.x.x and the other is IDE 2.x. The IDE 2.x is new major release that is faster and even more powerful to the IDE 1.x.x. In addition to a more modern editor and a more responsive interface it includes advanced features to help users with their coding and debugging.

The following steps can guide you with using the offline IDE:

1- Download and install the Arduino Software IDE
Click here to download

2- Connect your Arduino board to your device.

3- Open the Arduino Software (IDE).

4- select the correct board from the list.

5- Make sure you choose these options.

6- this step to testing the board if it is works or not.

We have made some adjustments such as changing the turning off and turning on time.

in this case we made the micro controller light up for 3 seconds and turn off for one second.

Here you can see the image of the code and the video that shows the micro controller light.

After that we did a challenge,I have chosen 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. (Refer to the schedule).

This is the schedule:

Here you can see my solution

// 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{} {

digitalWrite(LED_BUILTIN, HIGH);
delay (1000);
digitalwrite(LED_BUILTIN, LOW);
delay (3000);
digitalvrite(LED_BUILTIN, HIGH);
delay (2000);
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 (1000);
digitalwrite(LED_BUILTIN, LOW);
delay (500);
digitalwrite(LED_BUILTIN, HIGH);
delay (1000) ;
| digitalWrite(LED_BUILTIN, LOW);
delay (500);
}

My word was END and here you can see in the video how the micro controller work with this code.

Using the Thonny Software:

Thonny is an integrated development environment (IDE) for Python programming. It’s designed to be simple and beginner-friendly while also offering powerful features for more advanced users. Thonny provides an environment where you can write, execute, and debug Python code efficiently.
Here are some key features:

  • Integrated Debugger: Thonny includes a built-in debugger that helps you track down and fix bugs in your Python code. It allows you to step through code line by line, inspect variables, and understand program flow.

  • Syntax Highlighting and Autocompletion: Like many IDEs, Thonny offers syntax highlighting to make your code easier to read, and autocompletion to speed up writing by suggesting completions for functions, variables, and modules.

  • Variable Explorer: Thonny includes a variable explorer that lets you view and interact with variables in your program, making it easier to understand how data is manipulated as your program runs.

The following steps can guide you with using Thonny software:

1- Download and install the Arduino Software IDE
Click here to download

2- Open the Thonny Software

3- Connect your board to your device.

4- select which kind of interpreter.

5- for our board select these options.

6- To make the lamp light up for infinity, you can use this code.

from machine import Pin
import time

LED = Pin(26, Pin.OUT)

while True:
    LED.value(1) 
    time.sleep(2) 
    LED.value(0)  
    time.sleep(1)

7- To make the lamp light up randomly, you can use this code.

from machine import Pin
import time
import random

LED_PIN = 26
LED = Pin(LED_PIN, Pin.OUT)

MIN_ON_DURATION = 0.5
MAX_ON_DURATION = 3.0
MIN_OFF_DURATION = 0.5
MAX_OFF_DURATION = 3.0

def get_random_duration(min_duration, max_duration):
    return random.uniform(min_duration, max_duration)

while True:
    on_duration = get_random_duration(MIN_ON_DURATION, MAX_ON_DURATION)
    off_duration = get_random_duration(MIN_OFF_DURATION, MAX_OFF_DURATION)

    LED.value(1)  
    time.sleep(on_duration) 

    LED.value(0) 
    time.sleep(off_duration)

8- To give the user the freedom to choose the time of turn on and turn off,you can use this code.

from machine import Pin
import time

LED_PIN = 26  
LED = Pin(LED_PIN, Pin.OUT)

def blink_led(on_duration, off_duration):
    while True:
        LED.value(1) 
        time.sleep(on_duration)  
        LED.value(0)  
        time.sleep(off_duration)  

on_duration = float(input("Enter duration for LED ON (seconds): "))
off_duration = float(input("Enter duration for LED OFF (seconds): "))

blink_led(on_duration, off_duration)

I have tried to connect the button ,lamp,fan and buzzer all together

Here you can see the code and the video of this experiment

from machine import Pin
import time

   # Define pin numbers
BUTTON_PIN = 27
MOTOR_PIN = 2
LED_PIN = 26
BUZZER_PINS = [12, 13, 14]  # List of buzzer pin numbers

     # Initialize pins
button = Pin(BUTTON_PIN, Pin.IN)
motor = Pin(MOTOR_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)
buzzers = [Pin(pin, Pin.OUT) for pin in BUZZER_PINS]  # Initialize buzzer pins

      # Function to control motor, LED, and buzzers
     def control_motor_led_and_buzzers(state):
    motor.value(state)
    led.value(state)
    for buzzer in buzzers:
        buzzer.value(state)

    # Main loop
      while True:
    # Check if button is pressed (button.value() will be 0 when pressed)
    if button.value() == 0:
        control_motor_led_and_buzzers(1)  # Turn motor, LED, and buzzers on
    else:
        control_motor_led_and_buzzers(0)  # Turn motor, LED, and buzzers off

    # Add a small delay to avoid checking the button state too frequently
    time.sleep(0.1)
<iframe width="100%" height="315" src="https://www.youtube.com/embed/AytvjctkX9U" frameborder="0" allowfullscreen></iframe>

Last update: July 28, 2024