Embedded programming

Group assignment

The group assignment involved delving into the datasheet for the Xiao SAMD21 microcontroller, comparing its performance and development workflows with other architectures, and documenting the findings.

First, the microcontroller's specs were examined, detailing its pin layout, peripherals, and features like CPU, flash memory, and SRAM.

Next, a comparison with other microcontrollers like Arduino Mega, Arduino Uno, and Raspberry Pi RP2040 Zero was conducted. Each was assessed based on factors like bit size, max clock speed, I/O pins, storage, operating voltage, connectivity, and price. Pros and cons were outlined for each, highlighting their suitability for various projects.

Lastly, the development workflow for programming each microcontroller was explained. This involved setting up the Arduino IDE and writing a simple blink sketch for each board. The process for Xiao SAMD21, Arduino Uno, and Arduino Mega was similar, utilizing the Arduino IDE and C/C++ programming language. Meanwhile, programming the Raspberry Pi RP2040 Zero also involved the Arduino IDE but required additional steps for installing the Arduino Core and board support package.

In summary, the assignment provided a comprehensive understanding of the Xiao SAMD21 microcontroller, its comparison with other architectures, and the development workflows for programming different microcontrollers.


Individual assignment

LED Control Code on Seeed SAMD Microcontroller

Overview

This code is designed to control an LED connected to a Seeed SAMD microcontroller. The LED is turned on and off based on the duration specified through serial input. The duration for both the ON and OFF states of the LED is provided by the user via serial communication.

To program the Xiao board using the Arduino IDE, you'll need to install the appropriate board definitions and drivers.

Here are the general steps to install the board definitions for the Xiao board in the Arduino IDE:

  1. Open the Arduino IDE.
  2. Go to File > Preferences (on Windows) or Arduino > Preferences (on macOS).
  3. In the "Additional Board Manager URLs" field, add the following URL if it's not already present: https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json
  4. Click "OK" to close the Preferences window.
  5. Go to Tools > Board > Boards Manager.
  6. In the Boards Manager window, type "Seeed SAMD Boards" in the search bar.
  7. Click on the "Seeed SAMD Boards" entry, and click the "Install" button.
  8. Once the installation is complete, close the Boards Manager window.

After installing the board definitions, you should be able to select the Seed Studio Xiao board from the Tools > Board menu in the Arduino IDE and proceed with uploading sketches to it.

As for drivers, the Xiao board should use the built-in USB CDC (Communications Device Class) drivers provided by the Arduino IDE for communicating with your computer. Usually, when you connect the Xiao board to your computer via USB for the first time, it should automatically install the necessary drivers. However, if you encounter driver issues, you may need to check the official documentation or forums provided by Seed Studio for any specific driver installation instructions or troubleshooting tips.

Always make sure you are referring to the latest documentation and resources provided by Seed Studio for the most up-to-date information on using the Xiao board with the Arduino IDE.

Hardware Setup

  • Microcontroller: Seeed SAMD (such as Seeeduino XIAO)
  • LED: Connected to digital pin 0
  • Resistor: 220Ω (to limit current to the LED)
  • Connections:
    • LED anode (+) connected to digital pin 0
    • LED cathode (-) connected to one end of the resistor
    • Other end of the resistor connected to GND

Code Explanation

Define the Pin Connected to the LED

cpp
const int ledPin = 0;
  • ledPin: Defines the digital pin (0) connected to the LED.

Setup Function

cpp
void setup() { // Set the LED pin as an output pinMode(ledPin, OUTPUT); // Start serial communication Serial.begin(9600); }
  • pinMode(ledPin, OUTPUT): Configures the pin connected to the LED as an output.
  • Serial.begin(9600): Initializes serial communication at a baud rate of 9600 bps.

Loop Function

cpp
void loop() { // Variable to hold the duration of the light being on and off int onOffDuration; // Check if data is available in the serial buffer if (Serial.available() > 0) { // Read the data from the serial buffer onOffDuration = Serial.parseInt(); // Turn on the LED digitalWrite(ledPin, HIGH); // Delay for the specified duration delay(onOffDuration); // Turn off the LED digitalWrite(ledPin, LOW); // Delay for the specified duration again delay(onOffDuration); } }
  • int onOffDuration: Declares a variable to store the duration for which the LED will be on and off.
  • if (Serial.available() > 0): Checks if data is available in the serial buffer.
  • onOffDuration = Serial.parseInt(): Reads the integer value from the serial buffer and assigns it to onOffDuration.
  • digitalWrite(ledPin, HIGH): Turns the LED on.
  • delay(onOffDuration): Keeps the LED on for the specified duration.
  • digitalWrite(ledPin, LOW): Turns the LED off.
  • delay(onOffDuration): Keeps the LED off for the specified duration.

How to Use the Code

  1. Upload the Code: Upload the code to the Seeed SAMD microcontroller using the Arduino IDE.
  2. Open Serial Monitor: Open the Serial Monitor in the Arduino IDE.
  3. Input Duration: Enter the duration in milliseconds in the Serial Monitor and press enter. For example, entering 1000 will turn the LED on for 1 second and then off for 1 second repeatedly.

Example Usage

If the user enters 500 in the Serial Monitor:

  • The LED will turn on for 500 milliseconds.
  • The LED will turn off for 500 milliseconds.
  • This cycle will continue until a new duration is entered or the microcontroller is reset.

This code provides a simple way to control the blinking of an LED based on user input via serial communication.


LED Control with Seeed SAMD Microcontroller Documentation

Overview

This document provides an overview and detailed explanation of a program designed to control three LEDs connected to a PCB board interfacing with a Seeed SAMD microcontroller. The LEDs are connected to pins 0, 6, and 7 and are controlled to turn on and off in sequence with a specified delay between each state change.

Components

  • LEDs: 3 LEDs mounted on a PCB board.
  • Resistors: Appropriate resistors for each LED to limit current and prevent damage.
  • Seeed SAMD Microcontroller: A compatible microcontroller (e.g., Seeed SAMD21) connected to the PCB.
  • PCB Board: Custom PCB board with traces connecting the LEDs to the specified pins on the microcontroller.

Code Explanation

Variable Declarations

cpp
const int ledPin1 = 0; const int ledPin2 = 6; const int ledPin3 = 7; const int delayTime = 200;
  • ledPin1, ledPin2, ledPin3: Constants representing the pins to which the LEDs are connected.
  • delayTime: A constant representing the delay (in milliseconds) between state changes of the LEDs.

Setup Function

cpp
void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); }
  • Purpose: This function is called once when the program starts.
  • Functionality: Sets the pins ledPin1, ledPin2, and ledPin3 as output pins, enabling them to control the LEDs.

Loop Function

cpp
void loop() { // Turn on LEDs in sequence digitalWrite(ledPin1, HIGH); delay(delayTime); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); delay(delayTime); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, HIGH); delay(delayTime); digitalWrite(ledPin3, LOW); // Turn off LEDs in reverse sequence digitalWrite(ledPin3, HIGH); delay(delayTime); digitalWrite(ledPin3, LOW); digitalWrite(ledPin2, HIGH); delay(delayTime); digitalWrite(ledPin2, LOW); digitalWrite(ledPin1, HIGH); delay(delayTime); digitalWrite(ledPin1, LOW); }
  • Purpose: This function runs continuously, controlling the sequence of LED states.
  • Functionality:
    • Sequence On: The LEDs connected to ledPin1, ledPin2, and ledPin3 are turned on and off one by one with a delay of delayTime milliseconds between each state change.
    • Reverse Sequence Off: The LEDs are then turned on and off in reverse sequence with the same delay.

Circuit Diagram

To properly connect the LEDs on the PCB board to the Seeed SAMD microcontroller, follow this setup:

  1. PCB Board Connections: Ensure the PCB board traces connect the anode (long leg) of each LED to the corresponding pins (0, 6, 7) on the microcontroller.
  2. Resistors: Include resistors on the PCB board to limit current for each LED.
  3. Ground Connections: Ensure the cathode (short leg) of each LED is connected to the ground (GND) on the microcontroller.

Conclusion

This program effectively demonstrates basic LED control using a Seeed SAMD microcontroller, allowing LEDs to turn on and off in a sequence with a specified delay. The provided code is a fundamental example and can be extended for more complex light patterns and behaviors.

Code Listing

Here is the complete code for reference:

cpp
const int ledPin1 = 0; const int ledPin2 = 6; const int ledPin3 = 7; const int delayTime = 200; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); } void loop() { // Turn on LEDs in sequence digitalWrite(ledPin1, HIGH); delay(delayTime); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); delay(delayTime); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, HIGH); delay(delayTime); digitalWrite(ledPin3, LOW); // Turn off LEDs in reverse sequence digitalWrite(ledPin3, HIGH); delay(delayTime); digitalWrite(ledPin3, LOW); digitalWrite(ledPin2, HIGH); delay(delayTime); digitalWrite(ledPin2, LOW); digitalWrite(ledPin1, HIGH); delay(delayTime); digitalWrite(ledPin1, LOW); }

This documentation provides a clear understanding of the setup, code, and functionality for controlling LEDs using a Seeed SAMD microcontroller.

Helpful links

  1. Control the LED by the serial monitor
  2. Control three LED