Back
Featured image of post Embedded Programming

Embedded Programming

Week 6

This week brought an introduction to the wide (and perhaps daunting..) world of embedded programming. We covered general topics and computer theory such as the difference between analog and digital (continuous vs discrete values).

We live in an analog world- imagine time, where there are an infinite number of possible values. The digital world of computers and machines, however, is built on just two values: 0 and 1. Imagine a switch, which is either on or off. Digital logic is the idea of encoding analog values as digital ones.

Logic operators/gates (where two inputs are used to determine one output) can be can be used to build programs- sets of actions or outputs in response to inputs. All digital devices are built using these logic gate building blocks. A website called SimpleCPU.com is a nice introductory resource to working with binary data and the basics of computer theory.

We covered combinatorial vs sequential circuitry and built sample circuits of an AND gate and an OR gate on breadboards.

OR logic gate on a breadboard
OR logic gate on a breadboard

Assignments

Our tasks for this week are:

Group assignment:

  • Browse through the data sheet for your microcontroller
  • Compare the performance and development workflows for other architectures

Individual assignment:

  • Write a program for a microcontroller development board that you made, to interact (with local input &/or output devices) and communicate (with remote wired or wireless devices) extra credit: use different languages &/or development environments extra credit: connect external components to the board

Group Assignment:

Fab Lab BCN webpage for the embedded programming group assignment can be found here.

Browse through the data sheet for your microcontroller

There are many options for microcontrollers, but in general in Fab Academy there are a few key families that are utilized.

The general takeaway for this week is to choose a microcontroller to fit the scope of the project - you don’t need to go overkill with a super powerful uC that you then underutilize. Better to select a uC that is sufficient for your purposes.

ARM

“SAM D” microcontroller examples: SAM D11, SAM D21

  • has a USB interface, don’t need UDPI to program
  • SAMD11 requires that you upload a bootloader to it to be able to understand USB/turn on the USB interface, but the bootloader takes up a lot of memory. So only use this microcontroller if you don’t need USB capabilities, or touch capacity

Atmel

  • make the ATTiny family of microcontrollers
  • new generation ATTiny boards - 412, 1614, 3216 - are small but can do a lot. Due to their small memory size, they’re not a good choice for a project where you need to connect a lot of things…you can overload them.
  • As a class we browsed through the datasheet for the ATTiny412.

Microcontroller datasheets are incredibly thorough and dense - they contain all the possible information you could hope to know about the microcontrollers. The datasheet for this tiny microcontroller that costs less than a dollar is 479 pages long.

Features such as CPU, memory types and amounts, system attributes (such as power on, clock options, UPDI, sleep modes), peripherals (counter, timer, communications, ADC, DAC, voltages, inputs/outputs), I/O and packages, operating temperatures, and speed grades are summarized at the beginning of the document. One key bit of information to look for in these documents is how to program the board- in this case the ATTiny412 is programmed with UDPI (Unified Program Debug Interface). The block diagram and Pinout diagram were other sections of the datasheet which were highlighted.

I’m curious to apply the AI PDF decoder mentioned in the AI recitation to quickly gather relevant pieces of information from these long documents, but I know there’s still the caveat that any information I get from AI will need to be double checked.

RP2040

This is the microcontroller used in the XIAO seeed development board (which we used in our Quentorres dev boards).

ESP boards

  • The ESP32 has great networking capabilities, offering both bluetooth and wifi
  • ESP32-S3 is the uC in the Barduino - it has many capabilities and is easy to program due to the direct USB interface, but its large size can be a limitation
  • ESP 32-C3 is like the ESP but with RISC-V open architecture

Microprocessor Architecture Comparision

There are two main types of microcontroller architecture: Harvard architecture and von Neumann architecture.

Harvard architecture Von Neumann architecture
Data bus & instruction are separate One bus is used for both data and instructions
allows simultaneous transfers doesn’t allow simultaneous transfers

RISC-V is a newer architecture which is interesting because it’s open source. Will be a good one to keep an eye on…

Individual Assignment

Arduino IDE

In our local class we covered connecting with the serial monitor and serial plotter within the Arduino IDE.

Accessing and using the Serial Monitor
Accessing and using the Serial Monitor

Video: flashing an LED on and off and displaying “On” or “Off” on the serial monitor

I used the Arduino IDE to program my Quentorres dev board (which used the seeed XIAO RP2040). By updating the blink example code (modifying the code to the correct pin output of D0, where the LED is connected) I was able to successfully get an LED on my board to turn on when I pressed the button down.

Pressing a button to turn an LED on, releasing the button turns the LED off again
/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
const int ledPin = D0;
const int button1 = D1;


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(ledPin, OUTPUT);
  pinMode(button1, INPUT);
//  digitalWrite(ledPin, HIGH);
}

// the loop function runs over and over again forever
void loop() {
  int button1check = digitalRead(button1);
  if (button1check == HIGH)
  {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
//  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
//  delay(1000);                       // wait for a second
//  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
//  delay(1000);                       // wait for a second
}

I also updated/modified the program for the Barduino board each of us recieved when we started at Fab Lab BCN, in both the Arduino IDE as well.

Adding the Barduino board microcontroller (ESP32)to the Arduino IDE
Adding the Barduino board microcontroller (ESP32)to the Arduino IDE

A program on the Barduino board to make an LED fade on and off

Programming with MicroPython in Thonny

Installing microPython on the RP2040
Installing microPython on the RP2040

Reflections

The Arduino IDE seems like an easy-to-use interface for familiarizing myself with the process of embedded programming. The one downside is how long it can take to compile code, but I think the large community of users, available libraries, and general support are worth the wait for me at this point. I also found working with microPython in Thonny to be not very painful, and much faster to see updates in your code on the board, but I think I’ll move forward with the Arduino IDE.

In addition to dipping my toes into embedded programming, this week I attempted to cut copper traces using the vinyl cutter. It was a challenge due to the tendency of the copper, especially when in such fine traces, to detach from the tape backing. We played with lowering the speed and increasing the cutting force, but still struggled with the small traces dislodging. We had a breakthrough by using a layer of transfer paper over the copper tape prior to cutting, but need to continue to fiddle with the settings (and likely the knife depth) in order to cut fully through the copper tape & transfer paper layers.

Built with Hugo | Using a personally adjusted version of Nadieh Bremer's website and the theme Stack