6. Embedded Programming¶
This week I expanded my knowledge of microcontrollers, their capabilities including their communication with external devices, and how to program some smaller types of microcontrollers. As a part of this, I worked in a group with Landon Broadwell and Ryan Zhou, the documentation for which can be found here.
Input and Output/Communication with External Device¶
In Week 4, I programmed my SEEED STUDIO RP2040 board to control three LEDs, including using a button to control these LEDs. However, when I found my board again, it did not work, so I milled and soldered a new one.
After soldering it and being 150 miles from my in-house milling machine on vacation, I realized that the outline had cut off one of the traces, one which happened to be one of the two most vital to the assignment. After an absurd amount of time of searching for a better make-shift solution, I simply connected the two disconnected traces with a piece of solder which I clamped down on one side and held down with my finger on the other. I modified my previous code so that the button acted like a switch by setting the LEDs states to !digitalRead([LED pin])
, basically activating the opposite of its previous state. I added in Serial.begin(9600);
in the void setup
of my Arduino IDE code and Serial.println("Button has been pressed.");
into the if statement for the button. In addition, I took inspiration from Richard Shan’s code, inserting a variable called “lastbuttonState” which, when included in the conditional statement, basically only allows “Button has been pressed.” to be printed once when the button is pressed. Below is the final code.
int lastbuttonState = LOW;
void setup() {
// Initialize LED pins as outputs
pinMode(1, OUTPUT); // Bottom right LED
pinMode(26, OUTPUT); // Top left LED
pinMode(0, OUTPUT); // Bottom left LED
// Initialize the button pin as an input
pinMode(27, INPUT_PULLUP); // Button tactile switch on pin 27
Serial.begin(9600);
}
void loop() {
// Read the state of the button
int buttonState = digitalRead(27);
if (buttonState==HIGH && lastbuttonState==LOW){
digitalWrite(1, !digitalRead(1)); // Turn on bottom right LED
digitalWrite(26, !digitalRead(26)); // Turn on top left LED
digitalWrite(0, !digitalRead(0)); // Turn on bottom left LED
Serial.println("Button has been pressed.");
delay(10);
}
lastbuttonState=buttonState;
}
After sending this code to the RP2040 via USB-C, I opened the Serial Monitor in Arduino IDE by selecting Tools>Serial Monitor. When I pressed the button, it worked!
SAMD11C¶
In addition to the regular weekly assignment, Mr. Dubick assigned us to program two different types of microcontrollers. We were split into four groups, and two groups did each microcontroller. My group was assigned to the SAMD. First, we had to mill the SAMD board. Initially, we milled the SAMDino board, but when we discovered the unnecessary complexity of both the soldering and programming of this board, we switched to the more simplistic Hello Board. The information and files for both of these boards were mostly acquired solely from Adrian Torres’ documentation. After milling and soldering the Hello Board, we assumed that we were supposed to program and code the SAMD11C through the RP2040 boards we had already created, as this is what Mr. Dubick told us to do, and this is what the other groups (working with the ATTiny412 board) managed to do. However, we could not find any previous documentation doing this.
Flashing EDBG¶
Eventually, using Adrian Torres’ documentation, Richard was able to flash the .BIN file to a SAMD11C board through an RP2040 board using Windows Powershell. The .BIN file is a simple, binary file used to allow the SAMD11C chip to be recognized and coded by the computer. He did this through the following steps:
- Using a 2x5 connector cable to the Hello Board to the circuit board holding the RP2040
- Plugging the RP2040 board into a computer via a USB-C cable
- Hold down on the B button and click the R button on the RP2040 to send it into bootloader mode
- Upload the free_dap_rp2040.uf2 file (found on the Fab Academy schedule) to the RP2040 which should appear in file navigator
- Download the most recent Windows version of EDBG
- Install the .BIN file from here
- Create a folder which holds both the .BIN file and the EDBG executable
- Open Windows Powershell and navigate to the EDBG folder
- Run the following command “.\edbg-windows-r61.exe -b -p -v -e -t samd11 -f .\sam_ba_Generic_D11C14A_SAMD11C14A.bin”
More detail on the meaning behind different components of this command and the origins of many of the aforementioned files can be found on Richard’s documentation.
Coding Hello Board¶
Although we were unable to find a way to code the board through the RP2040, we were able to simply code it directly instead. Richard and I found that one can plug the four-pronged protruding end of the Hello Board into a USB port, and it will act just like a USB. By doing this, Richard pulled up a basic blink code on Arduino IDE, added the below board URLs to the boards manager in Arduino IDE, selected the “Generic D11C14A” as the board, and selected the only available port for the port.
https://raw.githubusercontent.com/qbolsee/ArduinoCore-fab-sam/master/json/package_Fab_SAM_index.json
https://www.mattairtech.com/software/arduino/package_MattairTech_index.json
It uploaded smoothly, and it worked!
ATTiny412¶
Because my group was not assigned to the ATTiny412 microcontroller, we were not responsible for the milling, soldering, or general setup of the board and its various components. In addition, Collin Kanofsky assisted me in the final steps of uploading code to this board. I first added the following library to the library URL in Arduino IDE:
http://drazzy.com/package_drazzy.com_index.json
void setup() {
pinMode(4, OUTPUT); // defines PIN 4 (LED) as an output
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(4, HIGH); // turns the LED from PIN 4 on
delay(1000); // delays one second
digitalWrite(4, LOW); // turn the LED from PIN 4 off
delay(1000); // delays one second
}
Then, under Tools, I selected ATTiy412 under Boards, the programmer shown below, and the correct port.
I then selected Upload Using Programmer under Sketch. Finally, I could upload the code.
Week Reflection¶
This week was another week which only seems complicated in the process, but is in reality fairly simple. I learned a lot about electronics this week, a realm which I had little experience with in the past.
File Downloads¶
You can download the file downloads for files with downloads not already included in this documentation or any referenced documentation here.