8. Embedded programming¶
Group assignment:
- Compare the performance and development workflows for other architectures
Individual assignment:
-
Read the data sheet for your microcontroller
-
Use your programmer to program your board to do something
-
extra credit: try other programming languages and development environments
Have you answered these questions?
- Linked to the group assignment page
- Documented what you learned from reading a microcontroller datasheet.
- Programmed your board
- Described the programming process/es you used
- Included your source code
- Included a short ‘hero video’ of your board
What I’ve done this week¶
Group assignment:
Individual assignment:
- Read the data sheet for ATTiny412
- Use FT230X to program ATTiny412 board to press the button to make the LED light up
- Read the data sheet for ESP32
- Use FT230X to program ESP32 (redraw Barduino2.0))for final project
Read the data sheet for ATTiny412¶
I used ATTiny412 for this assignment
The name “ATTiny412” tell the Flash capacity, number of pins, etc.
SOIC (Small Outline IC Package) is a leadframe-based package suitable for applications requiring optimal performance in IC packaging.
The following is the correspondence table with the pin assignments in Arduino
PA3 is Pin 4 in Arduinoand PA7 is Pin 1 in Arduino. In this case, I connected an LED to PA3 (4) and a Button to PA7 (1)
Use FT230X to program ATTiny412 board to press the button to make the LED light up¶
Wiring was done as follows
The settings for writing the program are as follows
#define LED 4
#define Button 1
void setup() {
pinMode(LED,OUTPUT);
}
void loop() {
if (digitalRead(Button) == LOW) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
Read the data sheet for ESP32¶
I will use ESP32 as a web server in my final project to control stepper motor and servo motor.
So, I read the datasheet.
The pin assignment of ESP32 is as follows
Setting the EN pin to Low will cause a reset.
EN pin on pin 3 is an enable signal, it is Active High, so it mean “High: normal use, Low: power off”.
The operation setting of Strapping Pins is described as hold until power off, so reset is applied when EN pin is set to a low.
The board created this time (redraw barduino) uses a tact switch to handle the reset .
(also barduino2.0)
When the button is not pressed, it is pulled up because it is connected to 3.3V (EN is HIGH), but when clicked, EN goes LOW
The esp32 board created this time has a 3.3V regulator, which is recommended in the datasheet.
According to the datasheet, when operated at 3.3V, “LOW” means that the low-level input voltage is VDD-0.3 to 0.25 x VDD, and inputs between -0.3V and 0.825V are considered LOW.
“HIGH” has a high-level input voltage of 0.75 x VDD to VDD+0.3, which means that the voltage from 2.475V to 3.6V is “HIGH”.
Use FT230X to program esp32 board for final project¶
This time I created ESP32 board (redraw barduino2.0) to be used in Final project
Writing to the board with the Arduino IDE
When the message “Leaving… Hard resetting via RTS pin” appears, switch to run mode and press the reset button.
Wiring was done as follows
Written using FT230xs programmer
The settings for writing the program are as follows
The following code is for use in the Final project. it is still the test code.
ESP32 board will be used as a web server that receives HTTP GET requests from Raspberry Pi
Send the results of image recognition by Raspberry Pi to ESP32 by HTTP get request, and each processing will be done according to the received value.
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
#define SSID "ENV_WIFI"
#define PASS "ENV_PASS"
#define LED_PIN 13
Servo myservo;
int minUs = 500;
int maxUs = 2400;
WebServer server(80);
void setup(){
Serial.begin(9600);
myservo.setPeriodHertz(50);
myservo.attach(23, minUs, maxUs);
pinMode(LED_PIN, OUTPUT);
connectWiFi();
server.on("/", handleRoot);
server.on("/nothing", nothing);
server.on("/charcol", charcol);
server.on("/notcharcol", notcharcol);
server.on("/plastic", plastic);
server.on("/battery", battery);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}
void connectWiFi(){
WiFi.begin(SSID, PASS);
Serial.println();
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
void handleRoot(){
server.send(200, "text/plain", "Hello");
}
void nothing(){
Serial.println("nothing");
myservo.write(0);
server.send(200, "text/plain", "nothing");
}
void charcol(){
Serial.println("charcol");
myservo.write(60);
server.send(200, "text/plain", "charcol");
}
void notcharcol(){
Serial.println("notcharcol");
myservo.write(120);
server.send(200, "text/plain", "notcharcol");
}
void plastic(){
Serial.println("plastic");
myservo.write(180);
server.send(200, "text/plain", "plastic");
}
void battery(){
Serial.println("battery");
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
server.send(200, "text/plain", "battery");
}
Explanation of the code
Use these libraries
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
At compile time, all “LED” are replaced with a value of 13.
Same for SSID and password
#define SSID "ENV_WIFI"
#define PASS "ENV_PASS"
#define LED_PIN 13
Declaration of Servo object
Servo myservo;
Published values for SG90 (servo)
int minUs = 500;
int maxUs = 2400;
Create an instance of the web server to process the web server with the name server. The port number is set to the common port 80
WebServer server(80);
Explanation of the code in void setup()
The data transfer rate for serial communication was specified at 9600 bps(baud)
Serial.begin(9600);
Use 50hz servo
servo.attach(pin, min, max)
-
pin: the number of the pin that the servo is attached to
-
min (optional): the pulse width, in microseconds, corresponding to the minimum (0-degree) angle on the servo (defaults to 544)
-
max (optional): the pulse width, in microseconds, corresponding to the maximum (180-degree) angle on the servo (defaults to 2400)
myservo.setPeriodHertz(50);
myservo.attach(23, minUs, maxUs);
Configures the LED pin (13) to behave as an output
pinMode(LED_PIN, OUTPUT);
Connect WiFi
connectWiFi();
When starting the web server, define a function for each address.
server.on("/", handleRoot);
server.on("/nothing", nothing);
server.on("/charcol", charcol);
server.on("/notcharcol", notcharcol);
server.on("/plastic", plastic);
server.on("/battery", battery);
server.begin();
Serial.println("HTTP server started");
In void loop(), only “WebServer.handleClient()” is called, and the processing is described in the each function
void loop() {
server.handleClient();
}
In the following functions, the serial monitor displays the name of the specified garbage, operates the servo motor at the respective set value of the angle, and returns a response of 200
When used in the final project, a stepping motor will be added in addition to the servo motor.
servo.write() sets the angle of the servo and points the shaft in that direction.
void nothing(){
Serial.println("nothing");
myservo.write(0);
server.send(200, "text/plain", "nothing");
}
void charcol(){
Serial.println("charcol");
myservo.write(60);
server.send(200, "text/plain", "charcol");
}
void notcharcol(){
Serial.println("notcharcol");
myservo.write(120);
server.send(200, "text/plain", "notcharcol");
}
void plastic(){
Serial.println("plastic");
myservo.write(180);
server.send(200, "text/plain", "plastic");
}
Batteries cannot be thrown in the trash in Kuriyama Town, so blinking LED to warn
void battery(){
Serial.println("battery");
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
server.send(200, "text/plain", "battery");
}
This time, HTTP GET request from a browser. (In the final project, the raspberry pi makes an HTTP get request for the image recognition results)
What I learned¶
- It is very important to read the data sheet. I will get into the habit of reading datasheets for future input and output device weeks and for the Final Project as well.
Links to Files and Code¶
-
Arduino file
-
Kicad
-
esp32_redraw.kicad_sch [.kicad_sch]
-
esp32_redraw.kicad_pcb [.kicad_pcb]
-