Assignment
- Individual assignment:
- Read a microcontroller data sheet
- Program your board to do something, with as many different programming languages and programming environments as possible
- Group assignment:
- Compare the performance and development workflows for other architectures
Individual Assignment
megaTinyCore
To recognize ATtiny412 board, we need to install megaTinyCore by following the instruction from Spence Konde.
This board package can be installed via the board manager. The boards manager URL is:
http://drazzy.com/package_drazzy.com_index.json
-
File -> Preferences, enter the above URL in “Additional Boards Manager URLs”
-
Tools -> Boards -> Boards Manager…
-
Wait while the list loads (takes longer than one would expect, and refreshes several times).
-
Select “megaTinyCore by Spence Konde” and click “Install”. For best results, choose the most recent version.
After finishing the setting, we can start to program our board with the UPDI board which I built in Week 05 Electronics Production. Use the code from echo.ino.
#define max_buffer 25
static int index = 0;
static char chr;
static char buffer[max_buffer] = {0};
void setup() {
//Serial.swap(1);
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
chr = Serial.read();
Serial.print("hello.t412.echo: you typed \"");
buffer[index++] = chr;
if (index == (max_buffer-1))
index = 0;
Serial.print(buffer);
Serial.println("\"");
}
}
We can get the echo successfully. And we don’t want to swap the pin here, thus we blocked it out.
Another try with echo.c. Here is the key point.
For using the code we need to change the definition of serial_pin_out and serial_pin_in. Refer to PA6 and PA7, we used PIN6_bm and PIN67_bm instead. The reference could be found from here.
Platform IO on Visual Studio Code
In this part of the practice, I followed a tutorial from Youtube and quick start from PlatformIO.
On the Visual Studio Code (VSC), I could install different extensions to program Arduino.
And I chose the steadiest and most popular one, PlatformIO. After installed the PlatformIO:
-
Click the Home button
-
Create a new project
-
Select correct board
-
Go to src to find the main.cpp file
-
Program in main.cpp must #include <Arduino.h> head file
-
Click the Build button to verify the code
-
Upload the code (Blink example) to Arduino
In the terminal description, we can see that the PlatformIO will automatically detect the correct port.
Test with Serial Monitor
I first used the default serial to monitor the printing message. But the message was garbled, couldn’t be read. So I realized that the default monitor was reading the message at 9600 baud rate.
To change the baud rate, I should activate the plug icon on the right bottom and change the baud rate to 115200. Then the data is readable.
Differences from Arduino IDE
- The main code is saved into cpp, instead of .ino
- #include <Arduino.h> need to be added on the top of code all the time
- Automatically detects the port
- Fancy user interface
- Different displayer of the serial monitor
- Heavier project file 713kb (blink), it’s only 7kb in normal Arduino IDE
- Arduino IDE needs to be installed anyway
Highlight from ATtiny412 Datasheets
In this section, I will introduce ATtiny412 which I used a lot during these weeks.
Section 2 - Series Overview
Section2 indicates the overview of tinyAVR 1-series.
From the table, we can see the 4 from 412 represents the size of the flash, and 12 represents 8 pins.
In the Peripheral Summary, I could understand Pins, ADC, DAC, and General purpose I/O. The rest of them I didn’t hear it before, so I google it:
-
Flash: A microcontroller typically has 3 types of memory: Flash, RAM, and EEPROM. Flash is a type of nonvolatile memory; that is, it’s a memory that keeps the information transferred to it even when the power to the microcontroller is shut off.
-
EEPROM stands for electrically erasable programmable read-only memory and is a type of non-volatile memory used in computers, integrated into microcontrollers for smart cards and remote keyless systems, and other electronic devices to store relatively small amounts of data by allowing individual bytes to be erased and reprogrammed.
-
SRAM: Static random-access memory is a type of random-access memory (RAM) that uses latching circuitry (flip-flop) to store each bit. SRAM is volatile memory; data is lost when power is removed.
-
USART stands for Universal Synchronous/Asynchronous Receiver/Transmitter, it is a microchip that facilitates communication through a computer’s serial port using the RS-232C protocol.
-
SPI: The Serial Peripheral Interface (SPI) is a synchronous serial communication interface specification used for short-distance communication, primarily in embedded systems.
-
TWI(I2C): I2C (Inter-Integrated Circuit, eye-squared-C) and is alternatively known as IIC. It is a synchronous, multi-master, multi-slave, packet-switched, single-ended, serial communication bus invented in 1982. It is widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication.
Section 3 - Block Diagram
Section 3 gives me a peek into Block Diagram, I could only recognize some of the words, but most of the terminologies are still like spells for me.
Section 4 - Pinout
This section was used a lot before this week, however, we referred much more to SpenceKonde where combine section 4 and section 5 together clearly.
Section 6 - Memories
Section 6 gives an expanded overview of the different types of memories; Flash, SRAM, and EEPROM. These can be separated into two types: Volatile memory where the stored data is lost when it loses power and Non-Volatile memory where it remembers the information even if the power to it is turned off. SRAM is volatile, while Flash and EEPROM are non-volatile.
Section 8 - AVR CPU
All AVR devices use the 8-bit AVR CPU. The CPU is able to access memories, perform calculations, control peripherals, and execute instructions in the programs memory.
The AVR CPU uses a Harvard architecture with separate buses for program and data.
Section 16 - PORT I/O Pin Configuration
The PORT supports synchronous and asynchronous input sensing with interrupts for selectable pin change conditions. Asynchronous pin-change sensing means that a pin change can wake the device from all sleep modes, including the modes where no clocks are running. All pin functions are configurable individually per pin.
The pins have hardware read-modify-write (RMW) functionality for safe and correct change of drive value and/or pull resistor configuration. The direction of one port pin can be changed without unintentionally changing the direction of any other pin.
Section 24 - USART / Universal Synchronous and Asynchronous Receiver and Transmitter
The communication that I’ve been using to receive and transmit (serial) data to my board via that TX and RX pins.
It is frame-based and a serial frame consists of:
- 1 start bit
- 5, 6, 7, 8, or 9 data bits
- Parity bit: even, odd, or none
- 1 or 2 stop bits
It can apparently also be set to one-wire mode, where the transmitter and receiver share the same TxD pin.
Section 30 - Analog to Digital Converter
Please refer to what I learn from Week 12 Input Devices
Section 32 - UPDI / Unified Program and Debug Interface
The Unified Program and Debug Interface (UPDI) is a proprietary interface for external programming and on-chip debugging of a device. It was mainly used for programming ATtiny Serial microcontrollers. But it does apply to all of the microcontrollers, for example, ESP-WROOM-02D needs to be programmed by FTDI.
Practical knowledge
The following points are what I learned, and further description could be found from Week12: Input Devices.
Bypass Capacitor (Decoupling Capacitors)
(picture from Eagle Academy)
?Vcc vs Vdd
In the datasheet and a lot of schematics, we can see Vcc and Vdd almost everywhere. I was so confused what the difference between them The answer from What is the difference between Vcc, Vdd, Vee, Vss? explains that “In practice today Vcc (collector or common collector voltage) and Vdd (drain) mean the positive voltage (+), and Vee (emitter) and Vss (source) means the ground (-)."
?Direction of LED
Program my own code
This part was finished by Week12: Input Devices.
long result; //variable for the result of the tx_rx measurement.
int analog_pin = 2; // PA1 of the ATtiny412
int tx_pin = 3; // PA2 of the ATtiny412
void setup() {
pinMode(tx_pin,OUTPUT); //Pin 2 provides the voltage step
Serial.begin(115200);
}
long tx_rx(){ //Function to execute rx_tx algorithm and return a value
//that depends on coupling of two electrodes.
//Value returned is a long integer.
int read_high;
int read_low;
int diff;
long int sum;
int N_samples = 100; //Number of samples to take. Larger number slows it down, but reduces scatter.
sum = 0;
for (int i = 0; i < N_samples; i++){
digitalWrite(tx_pin,HIGH); //Step the voltage high on conductor 1.
read_high = analogRead(analog_pin); //Measure response of conductor 2.
delayMicroseconds(100); //Delay to reach steady state.
digitalWrite(tx_pin,LOW); //Step the voltage to zero on conductor 1.
read_low = analogRead(analog_pin); //Measure response of conductor 2.
diff = read_high - read_low; //desired answer is the difference between high and low.
sum += diff; //Sums up N_samples of these measurements.
}
return sum;
} //End of tx_rx function.
void loop() {
result = tx_rx();
result = map(result, 6000, 11000, 0, 1024); //I recommend mapping the values of the two copper plates, it will depend on their size
Serial.println(result);
delay(100);
}
ESP32-CAM
I tried to use ESP32-CAM to get some videos.
First, I found a tutorial for having a brief introduction. Later for connecting FTDI cable and ESP32-CAM correctly, I found the datasheet of them.
ESP32-CAM Datasheet
Connect them with a breadboard and some cables.
Install the esp32 from Boards Manager, I learned from this website.
Open CameraWebServer example for testing.
I met a challenge that the program could be uploaded to the ESP-32 CAM.
So I googled for the solution. There is several solution and potential answers.
In conclusion,
-
I hold the RESET button while *Connecting…..
-
Change into a shorter USB cable.
-
Install a lasted driver from VCP Drivers
-
Modify the Flash Frequency: 40MHz and Flash Mode: into DIO
-
Change 3.3V VCC into 5V
-
Change Programmer: AVRISP mkll
And it worked!
But the weird thing is I couldn’t repeat the uploading workflow in the same setting condition. I found a comment that they said it might because of the bad quality of the USB cable or ESP32 itself.
Coding practice for DC motor
The further details could be found from Week14 Output Devices;
#define motorIN1 0 // IN1
#define motorIN2 1 // IN2
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(motorIN1, OUTPUT); //In hole
pinMode(motorIN2, OUTPUT); //Out hole
}
// the loop function runs over and over again forever
void loop() {
Forward();
Reverse();
}
void Forward(){
digitalWrite(motorIN2, 0);
// digitalWrite(motorIN1, 1); testing
// delay(5000);
for (int i=0; i<256 ; i++)
{
analogWrite(motorIN1,i);
delay(30);
}
for (int i=255; i>=0 ; i--)
{
analogWrite(motorIN1,i);
delay(30);
}
digitalWrite(motorIN1, 1); //break
digitalWrite(motorIN2, 1); //break
delay(100);
digitalWrite(motorIN1, 0); //standby
digitalWrite(motorIN2, 0); //standby
delay(100);
}
void Reverse(){
digitalWrite(motorIN1, 0);
for (int i=0; i<256 ; i++)
{
analogWrite(motorIN2,i);
delay(30);
}
for (int i=255; i>=0 ; i--)
{
analogWrite(motorIN2,i);
delay(30);
}
digitalWrite(motorIN1, 1); //break
digitalWrite(motorIN2, 1); //break
delay(100);
digitalWrite(motorIN1, 0); //standby
digitalWrite(motorIN2, 0); //standby
delay(100);
}
Group Assignment
Comparing ESP32-CAM, Aruino UNO REV3 and NodeMCU - ESP8266
ESP32-CAM | Aruino UNO REV3 | NodeMCU - ESP8266 | |
---|---|---|---|
Microcontroller | ATmega328P | ||
Operating Voltage | 3.3V or 5V | 5V | 3.3V |
Flash Memory | SPI Flash 32Mbit | 32 KB | 4MB |
RAM | built-in 520 KB+external 4MPSRAM | SRAM 2 KB | SRAM 64 KB |
IO port | 9 | Digital 14(6 provide PWM output) / Analog Input 6 | Digital 16 / Analog 1 |
Serial Port Baud-rate | Default 115200 bps | Default 9600 bps | |
Wi-Fi | 802.11b/g/n/e/i | 802.11b/g/n | |
Bluetooth | Bluetooth 4.2 BR/EDR and BLE standards | ||
Datasheet | DFRobot | Arduino Website | Components |
Download
The Week09 zip file includes:
- echo.c
- echo.ino
- Source code for the Esp32-CAM
- Code for DC motor
- Blink Code for PlatformIO