Skip to content

Week 4: Embedded Programming

In this unit, I used arduino ide except when I used micro python for which I used thonny

Arduino Uno

For Arduino, I chose to make a light blink based on distance from an ultrasonic sensor. I used the base code from the SR04 example and added LED commands to turn on along with printing the distance to the serial monitor. On the Arduino Uno, it is really easy to upload code. All you have to do is select the board under the Tools dropdown menu and select Arduino Uno, then select the port that it is plugged into (COM##). Next press upload and the code uploads to the Arduino.

full code:

//Dylan Ferro Fab Academy 2023
#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
  pinMode(13, OUTPUT);
   Serial.begin(9600);

}

void loop() {
   a=sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   if (a <= 10) {
    digitalWrite(13, HIGH);

   }
   else { 
    digitalWrite(13, LOW);
   }
}

Here is it working:

AtTiny 412 in Bare Metal

As per my teacher, Mr. Dubick’s instructions, I was to program the AtTiny 412 using bare metal code. This was difficult for me because I had no prior knowledge of how to code using bare metal. I searched for assistance from past graduates at my lab and Charles’s documentation helped me a lot. His code consisted of a fading LED and a blinking LED. I looked at his code on the blinking LED section and saw this code:

 PORTA.OUT |= PIN3_bm;             
 _delay_ms(250);                 
PORTA.OUT &= ~PIN3_bm;          
_delay_ms(250);            

This code turns on PIN3 in PORT A which is pin 7 on the 412 then turns it off. Here is the full code:

//Dylan Ferro Fab Academy 2023
#include <avr/io.h>
#include <util/delay.h>
void setup() {
  PORTA.DIRSET = PIN3_bm;  //set pin 7 on the 412 to an output pin.
  }
void loop() {
        PORTA.OUT |= PIN3_bm;             
        _delay_ms(250);                 
        PORTA.OUT &= ~PIN3_bm;          
        _delay_ms(250);                  
  }

Here is it working:

Uploading code to 412

To upload code to the 412, I used an Arduino Uno as my programmer. I used jtag2updi as the process. The first step to take is uploading jtag onto the Arduino to make it a programmer. then change the device to the Arduino 412, connect pin 6 on the Arduino to pin 6 on the 412, and upload the code

datasheet

RP2040

RP2040 In C++

Here is the datasheet, after reading it I learned the pin out of the rp2040 and the pins that are analog compatible vs the ones that are digital only. To go into depth more on reading data sheets, see my group work.

I used the Seeed XIAO RP2040 to program an LED to blink based on an ultrasonic sensor. I did this in C++ code. To connect the rp2040 to Arduino ide, first I went to the device manager and added the seeed rp2040. Then I plugged the rp2040 into my computer while holding down the boot button. this opened a folder labeled RPI-RP2. Then I went into the ports section of Arduino ide and selected UF2. then I uploaded my code, then I went back to the tools section and selected the correct COM port. Here is the code:

//Dylan Ferro Fab Academy 2023
#include "SR04.h"
#define TRIG_PIN 7
#define ECHO_PIN 0
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
  pinMode(1, OUTPUT);
   Serial.begin(9600);

}

void loop() {
   a=sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   if (a <= 10) {
    digitalWrite(1, HIGH);

   }
   else { 
    digitalWrite(1, LOW);
   }
}

Here is it working:

This code reads the distance with an ultrasonic sensor, prints it in the serial monitor, and blinks an LED if the distance is less than 10cm. Next, I programmed an RP2040 to blink an LED in Micro Python. For the pinout and other general information, go here.

Rp2040 in Micropython

To figure out how to use micropython, I used this tutorial. I decided that I would use a joystick since I will be using one for my final project. I will make the joystick display its position on the shell in thorny. To do this I searched how to make the code on Google. I found this website that set up the code perfectly. After reading the website and analyzing it, I was able to dissect the code into 3 main parts, the initial setup, the joystick initializing, and the if statements.

The initial setup:

#Dylan Ferro Fab Academy 2023
 from machine import Pin, ADC
import utime

xAxis = ADC(Pin(28))
yAxis = ADC(Pin(27))
button = Pin(26,Pin.IN, Pin.PULL_UP)

The joystick initialization:

#Dylan Ferro Fab Academy 2023
while True:
    xValue = xAxis.read_u16()
    yValue = yAxis.read_u16()
    buttonValue = button.value()
    xStatus = "middle"
    yStatus = "middle"
    buttonStatus = "not pressed"

The if statements:

#Dylan Ferro Fab Academy 2023
 if xValue <= 600:
        xStatus = "left"
    elif xValue >= 60000:
        xStatus = "right"
    if yValue <= 600:
        yStatus = "up"
    elif yValue >= 60000:
        yStatus = "down"
    if buttonValue == 0:
        buttonStatus = "pressed"
    print("X: " + xStatus + ", Y: " + yStatus + " -- button " + buttonStatus)
    utime.sleep(0.1)

Here is the full code:

#Dylan Ferro Fab Academy 2023
from machine import Pin, ADC
import utime

xAxis = ADC(Pin(28))
yAxis = ADC(Pin(27))
button = Pin(26,Pin.IN, Pin.PULL_UP)

while True:
    xValue = xAxis.read_u16()
    yValue = yAxis.read_u16()
    buttonValue = button.value()
    xStatus = "middle"
    yStatus = "middle"
    buttonStatus = "not pressed"
    if xValue <= 600:
        xStatus = "left"
    elif xValue >= 60000:
        xStatus = "right"
    if yValue <= 600:
        yStatus = "up"
    elif yValue >= 60000:
        yStatus = "down"
    if buttonValue == 0:
        buttonStatus = "pressed"
    print("X: " + xStatus + ", Y: " + yStatus + " -- button " + buttonStatus)
    utime.sleep(0.1)

Here is it working:

Reflection

When programming I found that Arduino IDE is much easier for me because of my history with the software. I think that MicroPython is a bit more difficult however it has its advantages. One of these is the lack of libraries needed to work and high compatibility with devices. In terms of bare metal, I think that it is very difficult however it did help me understand how basic coding works at a bare metal level. It almost displays a kind of skeleton of the well-known code I learned in Arduino ide.

Group Work and Files

For this week’s group work, click here, For my files, click here. This week I read the data sheet and found important information to fill out a graph on the chip we had, the tiny 1614. I also helped David with wiring the oscilloscope tests. On the datasheet I found some important information such as the max and min voltage, number of pinouts, and more, here is that information.

Microcontroller ATTiny1614
From Datasheet
Architecture TinyAVR
Operating System N/A
number of Cores 1
Core Clock speed 20 MHz
number of Registers 4
Register Size (8/16/32/64) 8
SRAM 2 KB
Flash 16 KB
EEPROM 256 bytes
Compatible Programming Languages UPDI
Programming Interface arduino, C, C++
Programming Workflow ”“”Sequence for Execution of Self-Programming In order to execute self-programming (the execution of writes to the NVM controller’s command register), the following steps are required: 1. The software temporarily enables self-programming by writing the SPM signature to the CCP register (CPU.CCP). 2. Within four instructions, the software must execute the appropriate instruction. The protected change is immediately disabled if the CPU performs accesses to the Flash, NVMCTRL, or EEPROM, or if the SLEEP instruction is executed. Once the correct signature is written by the CPU, interrupts will be ignored for the duration of the configuration change enable period. Any interrupt request (including non-maskable interrupts) during the CCP period will set the corresponding Interrupt flag as normal, and the request is kept pending. After the CCP period is completed, any pending interrupts are executed according to their level and priority”“”
number of I/O Pins 22
ADC/DAC (how many bits??) 2
PWM 16 bit single and dual slope
Operating Voltage Range 0.55V • 1.1V • 1.5V • 2.5V • 4.3V
Power Consumption 200 mA
Built-in Features CIPs, capacitive touch, PTC,
Oscilloscope Tests
Flip-Bit Speed Test Frequency 380.52kHz

Last update: August 24, 2024
Back to top