Skip to content

6. Embedded Programming - to fix

TO FIX - add serial monitor, no breadboard

This week I was tasked with investigating the datasheet for my microcontroller to get to know it better. And to compare it to other microcontroller architectures and document the comparison on our group page.

I also had to write a program for my microcontroller to interact with an external input or output.

Group work - comparing architectures

Our group work was to compare a few different microcontroller architectures. We documented our work here CDL Fab Lab - embedded programming group page.

The current test board I will be using is the Quentores Xiao RP2040 board that I created in week 4 - Electronics production since this is currently the first (and as of this week) only board I have made myself.

However, for “week 8 Electronics design” and my final project I intend to design and build my board around the ATtiny 412. This is for a few of reasons:

  • The ATtiny412 is a relatively powerful and CHEAP microcontroller, currently US $0.55, and we have allot of them in stock at our lab.

  • The ATtiny412 only has 8 pins, so I think the conplexity of the board design should be easier. And with simpler designs, there’s less chance for errors.

  • Despite it’s fewer pins and cheap cost I think it will be more than capapable of performing all the tasks I would want from it. If not, I can always upgrade to one of the higher Attiny family chips, but most of my knowledge and experience using the 412 should still be transferable.

  • The intension of our lab is to become a hub for entrepreneurs to develop prototypes for products that will hopefully become real world retail products. Most of the product ideas we have heard from past clients have only needed limited digital electronics needs, and price will probably be a major concern in picking thier parts list. For this reason I think the Attiny 412 will be well suited for the majority of them, and if not the 412, then another microcontroller in the Attiny family. To this end I would hope to be become well versed with them, and therefore will be focusing on the 412 for my Electronics design and final project.

  • The SEEED XIAO RP2040 microcontroller board that is used in my current test board is a little pricy (for my use). And we happen to have only 1 more spare in our lab.

Group work - addendum (RP2040 specs)

One of my lab mates, Terrence Carew, already documented the ATtiny412 pretty well on his page.

We initially didn’t document the RP2040 chip in our group page, so I decided to put some of the more relevant details here.

RP2040 datasheet

This is the Pin layout of the SEEED XIAO RP2040 microcontroller board Xiao pin layout

However the SEEED board already has allot of its pins assigned to other parts of the board like: built in LEDs, buttons, and USB interface.

The real (naked) RP2040 chip looks like this: RP2040 Pin layout RP2040 image

RP2040 Key features:

  • Dual ARM Cortex-M0+ @ 133MHz
  • 264kB on-chip SRAM in six independent banks
  • Support for up to 16MB of off-chip Flash memory via dedicated QSPI bus
  • DMA controller
  • Fully-connected AHB crossbar
  • Interpolator and integer divider peripherals
  • On-chip programmable LDO to generate core voltage
  • 2 on-chip PLLs to generate USB and core clocks
  • 30 GPIO pins, 4 of which can be used as analogue inputs
  • Peripherals
  • 2 UARTs
  • 2 SPI controllers
  • 2 I2C controllers
  • 16 PWM channels
  • USB 1.1 controller and PHY, with host and device support
  • 8 PIO state machines

RP2040 overview: RP2040 overview

Individual Assignment

This weeks assignment was to attach our test board to an external input or output device and then write a program to interface with that device.

I chose to use a basic analog potentiometer as an input and then use it to change the output of 2 LEDs on the test board.

I decided to use a potentiometer because I wanted to experiment with an analog input. Digital inputs seemed fairly straight forward and I thought that an analog interaction would be more interesting.

wiring

  • I used a breadboard just to hold the potentiometer in place and more easily attach the wires to it.
  • The red wire connects the potentiometer to the 5th spot on the female connection header (note: I didn’t have a 6 pos header available, so I used an 8 with the first 2 positions not connected to anything). This is wired to 5V VCC pin on the SEEED board.
  • The black wire connects the potentiometer to the 3rd spot on the header, which is connected to the GND of the SEEED board.
  • The Blue wire connects to the 6th spot on the header, which is wired to pin 28 on the SEEED board. This is set up as our analog input pin in the porgram.

potentiometer connected potentiometer connections

programming

I wrote the program in the Arduino IDE and used it to porgrammed the board.

I didn’t have to go through all the setup procedures for the Arduino IDE again because I alreay did them in week 4- electronic production.

program operation

The program takes in the voltage of the potentiometer as input. The dial is turned to increase the voltage coming out of the potentiometer signal(output) terminal.

As the dial is turned counterclockwize it increases the voltage signal value.

Every time the main body of the program loops the board reads the value of the voltage. The input voltage is interpreted as a value between 0 and 1023.

If the read value is between 0 and 350, the first LED blinks, while all others are off. The lower the value the faster the blinking.

If the read value is 350 or greater, the first LED stays on, and the 2nd LED blinks. The lower the value the faster the blinking.

my code

Here’s my code:

/*
  potentiometer - output to LEDs

  written 24 July 2024
  by James Khan
   for fab academy assignment embedded programming
  This example code is in the public domain.

  https://fabacademy.org/2021/labs/vancouver/students/james-khan/assignments/week06/
*/

// define constants to match the pin layout that you are attaching your potentiometer.
const int potentin = A3;
const int led1 = 26;
const int led2 = 0;
const int led3 = 1;

// setup our pins as inputs or outputs
void setup() {
  // initialize analog potentin pin as an input.
  pinMode(potentin, INPUT);

  // initialize digital led pins as output.
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

// the loop function runs over and over again forever
// analog input is from 0 to 1023. 1000 translates to 1 second delay. 
void loop() {
    // read potentiometer value
  int readvalue = analogRead(potentin);

  // reset all leds to off
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);

  if (readvalue < 350) {
    delay((readvalue));
    digitalWrite(led1, HIGH);
    delay((readvalue));
  }
  if (readvalue >= 350) {
      digitalWrite(led1, HIGH);
      delay(readvalue);
      digitalWrite(led2, HIGH);
      delay(readvalue);
  }

}

my potentiometer LED code (download)

My program running on my SEEED micropocessor board:

(Youtube link)

lessons learned

  • There are allot of microprocessors to pick from to use in your project. Each of them have differing strengths and weaknesses.
  • The Arduino IDE can be used for allot of boards, not just Arduino boards.
  • You need to define your input and output ports in your program as such to ensure that they work as intended.

Last update: November 10, 2024