Week 09: Embedded Programming

altpresent

Assignment

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
  1. File -> Preferences, enter the above URL in “Additional Boards Manager URLs”

  2. Tools -> Boards -> Boards Manager…

  3. Wait while the list loads (takes longer than one would expect, and refreshes several times).

  4. Select “megaTinyCore by Spence Konde” and click “Install”. For best results, choose the most recent version.

    alt

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.
alt alt

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.

alt alt alt alt

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.

default

And I chose the steadiest and most popular one, PlatformIO. After installed the PlatformIO:

  1. Click the Home button

  2. Create a new project

  3. Select correct board

  4. Go to src to find the main.cpp file

  5. Program in main.cpp must #include <Arduino.h> head file

    present
    present
  6. Click the Build button to verify the code

  7. Upload the code (Blink example) to Arduino

    present

In the terminal description, we can see that the PlatformIO will automatically detect the correct port.

present

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.

default

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.

default

Differences from Arduino IDE

  1. The main code is saved into cpp, instead of .ino
  2. #include <Arduino.h> need to be added on the top of code all the time
  3. Automatically detects the port
  4. Fancy user interface
  5. Different displayer of the serial monitor
  6. Heavier project file 713kb (blink), it’s only 7kb in normal Arduino IDE
  7. 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.

default

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:

Section 3 - Block Diagram

default

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.

default

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:

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

alt alt

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)

alt

(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

alt

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.

alt

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

alt
alt

Connect them with a breadboard and some cables.

alt alt

Install the esp32 from Boards Manager, I learned from this website.

alt alt

Open CameraWebServer example for testing.

alt

I met a challenge that the program could be uploaded to the ESP-32 CAM.

alt

So I googled for the solution. There is several solution and potential answers.

Github Tutorials

In conclusion,

  1. I hold the RESET button while *Connecting…..

  2. Change into a shorter USB cable.

  3. Install a lasted driver from VCP Drivers

  4. Modify the Flash Frequency: 40MHz and Flash Mode: into DIO

  5. Change 3.3V VCC into 5V

  6. Change Programmer: AVRISP mkll

    alt

    And it worked!

alt alt

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 altpresent
Aruino UNO REV3 altpresent
NodeMCU - ESP8266 alt
alt
alt
alt
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: