Embedded Programmings

For this sixth week of the Fab Academy course it is embedded programming, as they have been done over the past weeks there will be individual and group activities.

The Group assignment approaches browse through the datasheet for your microcontroller, compare performance and development workflows for other architectures, document your work on the group work page and reflect on your individual page what you learned.

Group assignment week 6

And the individual assignment was to write a program for a microcontroller development board to interact (with local input and/or output devices) and communicate (with wired or wireless remote devices).

what is embedded programming?

Embedded programming refers to the process of writing software specifically for embedded systems. Embedded systems are specialized computing systems that are designed to perform dedicated functions within larger mechanical or electrical systems.

Embedded programming involves writing code that is optimized to run on the limited resources (such as memory, processing power, and energy) available in embedded systems. This type of programming typically involves working closely with hardware components, including microcontrollers or microprocessors, sensors, actuators, and other peripherals.

C++

C++ is a general-purpose programming language that was developed as an extension of the C programming language. C++ supports both procedural and object-oriented programming paradigms, allowing developers to write modular and reusable code.

Python

Python is a high-level programming language known for its simplicity and readability.

Python is widely used in various fields such as web development, scientific computing, data analysis, artificial intelligence, machine learning, automation, and more.

Arduino

Arduino programming is done using a simplified version of the C++ programming language, specifically tailored for ease of use and quick prototyping. The Arduino language provides a set of pre-defined functions and libraries for controlling hardware peripherals, making it accessible even to beginners.

Here are some basic concepts to understand how to program in Arduino language:

Variables

Variables in Arduino store values that can change during program execution.

Control Structures

Control structures allow modifying the flow of program execution based on logical conditions.

Function

encapsulate a set of instructions that perform a specific task. They allow dividing the code into smaller, modular blocks.

Input and Output

input and output of data are typically done through digital and analog pins to interact with sensors, actuators, and other peripheral devices.

Microphyton

MicroPython is a software implementation of the Python 3 programming language, optimized to run on microcontrollers and embedded systems. MicroPython enables developers to write Python code that interacts with hardware.

Variables

Variables in MicroPython store values that can change during program execution. Data types specify what type of values a variable can hold.

Control structures

Control structures allow modifying the flow of program execution based on logical conditions.

Function

Functions encapsulate a set of instructions that perform a specific task. They allow dividing the code into smaller, modular blocks.

input and output

Input and output of data are typically done through GPIO pins to interact with sensors, actuators, and other peripheral devices.

Board

For this assignment I will be using the same board I made in week 4, the microcontroller I will use will be a XIAO-RP2040, these are some of its features:

XIAO-RP2040

Feature
RP2040
CPU Dual-core ARM Cortex-M0+
Clock speed Up to 133 MHz
Bit depth 32-bit
Storage 264KB of SRAM, and 2MB of on-board Flash memory
I/O pins 30 GPIO pins
Power 1.8V - 5.5V
Dimensions 20x17.5x3.5 mm
Bluetooth No
Wifi No
How to program USB mass-storage boot mode, drag-and-drop programming
Special Functions USB 1.1 Host/Device, 8 Programmable I/O (PIO) state machines

What am I going to do?

The code I developed this week operates as follows: upon pressing the button once, one LED illuminates; pressing it again triggers the second LED while extinguishing the first. Subsequent presses alternate LED activation and deactivation, with each press cycling to the next LED in sequence. Upon the fourth press, all LEDs illuminate simultaneously for one second before returning to their initial state.

Arduino code

1.- Declare variables

Here the variables are declared and initialized. Pin1, Pin2, and Pin3 store the pin numbers of three LEDs, while Button stores the pin number of the button. ButtonCounter counts the number of times the button has been pressed. lastDebounceTime records the time of the last bounce button, and debounceDelay sets he wait time to avoid the bounce, in milliseconds.

2.- Input and output

Sets up the pins associated with three LEDs as outputs and configures the button pin as an input with pull-down resistance in the development environment of a microcontroller

3.- preventing bouncing issues

It reads the current status of the button input and checks whether the button is pressed (i.e., if the status of the button is HIGH) and whether enough time has passed since the last press to disable any bounce. If both conditions are met, it increments the ButtonCounter variable to keep track of the number of button presses, and updates the lastDebounceTime variable to the current time using the millis() function.

4.- Led sequence

Here the code activates different LEDs based on the value of the ButtonCounter variable. If ButtonCounter is 1, it turns on the first LED for one second and then turns it off. If ButtonCounter is 2, it does the same for the second LED, and likewise for the third LED if ButtonCounter is 3. If ButtonCounter is 4, it turns on all three LEDs simultaneously for one second before turning them off. Finally, it resets ButtonCounter to 0.

This is the complete code

This is the program running using Arduino

Here you can downloadthe file

Microphyton code

1.- Declare variables

These lines define variables to represent the pin numbers for three LEDs (Pin1, Pin2, Pin3) and a button. ButtonCounter keeps track of the number of button presses, while lastDebounceTime and debounceDelay are used for debouncing the button.

Setup Function

The setup() function initializes the pins associated with the LEDs as outputs and the button pin as an input with pull-down resistor enabled.

Loop Function

The loop() function continuously checks the button state and increments ButtonCounter if the button is pressed and debounce conditions are met. Depending on the value of ButtonCounter, it toggles the LEDs accordingly: turning on each LED for one second and then turning it off. If ButtonCounter reaches 4, it turns on all LEDs simultaneously for one second and then turns them off, resetting ButtonCounter to 0.

This is the complete code

This is the program running using Micropython

Here you can downloadthe file