Assignment #8
Embedded Programming
The eighth Fab Academy assignment is embedded programming. We’ll learn to program a microcontroller to interact with other components.
1 – the parts
The coronavirus pandemic makes lab access difficult, so I’ll use an Arduino Uno clone called the Elegoo Uno for this assignment. I assembled a simple breadboard circuit with a green LED, a 220 ohm resistor, and a push-button switch, following the instructions in this tutorial
2 – The code
The code is simple: pressing the button turns the LED on; releasing it turns the LED off. First, I defined the pins connected to the components, then read the button’s two states and set the LED output accordingly:
// constants to define where led and button are connected:
const int BUTTON_PIN = 7; // the number of the pushbutton pin
const int LED_PIN = 3; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(LED_PIN, OUTPUT);
// initialize the pushbutton pin as an pull-up input:
// the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(BUTTON_PIN);
// control LED according to the state of button
if(buttonState == LOW) // If button is pressing
digitalWrite(LED_PIN, HIGH); // turn on LED
else // otherwise, button is not pressing
digitalWrite(LED_PIN, LOW); // turn off LED
}
3 – Upload the code
I used the Arduino IDE to write and upload the code. After selecting the board, I uploaded the sketch. On Linux, I had no problems with the clone’s drivers.
4 – testing the code
Once the code was uploaded I tried to push the button, and fortunately everything went smoothly…
5 – using the PCB I milled
Next, I’ll upload this code to the board I made during the Electronics Design week. I checked the controller’s datasheet and the AVRISP pin order to connect the board to the programmer I made during the Electronics Production week
In the Arduino IDE, I added ATtiny support by entering the board package URL in Preferences and installing the package through Boards Manager.
I then uploaded the code, which was essentially the same as the Arduino version:
6 – AVR experiment
Next, I tried uploading code using Atmel Studio. To learn the process, I looked at what Yasir Shafiullah did. I switched to Windows, installed the software, and followed these steps: - Install and open Atmel Studio 7- Start a new project by going to File > New > Project
- Select C/C++ as the language and choose the second project type in the list
- Select ATtiny as the device family, choose ATtiny44A, and click OK
- Go to Debug > GccApplication4 Properties and define the clock; in this example, the external clock is 20000000UL
- Add the programmer to the environment
- Write and upload the code
Using this workflow is far more difficult than using Arduino... here is the code:
