9. Embedded programming

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

ATtiny45 Board

The first activity of this week was to read a microcontroller datasheet, it was a very challenger task, three points caught my attention, the pin configuration, the architecture overview and the registers Configuration.

Image1- Pin Configuration

ATtiny45 datasheet is available here

Architecture overview

Image2- Architecture overview

To maximize performance and parallelism, the AVR uses a Harvard architecture – with separate memories and buses for program and data. Instructions in the Program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the Program memory. This concept enables instructions to be executed in every clock cycle. The Program memory is In-System Reprogrammable Flash memory.

Port B

Port B is a 6-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are tri-stated when a reset condition becomes active, even if the clock is not running.

Image3- Port B Alternate Functions

Ports I / O

  • The I / O ports have 3 registers: DDR, PORT, PIN.
  • All B ports on ATtiny45 have DDRB, PORTB, PINB registers.

DDR (Data Direction Register - read/write)

  • DDR is what sets the direction of the port whether it is an output or an input one. ( replace the function pinMode() )
  • In ATtiny45, we will add the channel of the B ports, becoming then DDRB (digital ports 0 to 5).
  • For each port is defined 1 if it is output, or if it is input, 0.
  • As it is a binary number, we must put the “B” modifier in front.
  • The order of the ports ranges from PB5 to PB0. Example: DDRB = B111110; // ports 1 to 5 are output, and port 0 is input.

PORT (Data Register - read / write)- logic state (High) or (Low)

  • In ATtiny45, we are going to add the channel of B ports to the PORT, thus becoming PORTB (digital ports 0 to 5).
  • Its logical state for each port is defined if it is HIGH, using 1 and if it is in LOW, using 0.
  • As it is a binary number, we must put the “B” modifier in front.
  • The order of the ports ranges from PB5 to PB0. Example: PORTB = B110001; // ports 5,4 and 0 are HIGH, and ports 1,2,3 are LOW.

PIN

  • PIN is a register whose value can be read but cannot be changed within the program.
  • It provides the actual voltage value for a specific pin. 5V corresponds to 1 and 0 corresponds to 0.

Image4- DDR, PORT

Advantages of using registers:

  • Access speed, as we will activate the door directly without the need for functions.
  • Optimization of microcontroller memory.
  • Efficient when we need to connect several ports at the same time.

Disadvantages of using registers

  • Code difficult to understand.
  • It is much easier to cause unintended malfunctions with direct access to the door.

Programming in C

In this program, the C language was used to program ATtiny45. In this programming we declare ports PB0 and PB2 as output, DDRB equal to 0b000101, and created a “while” that will be in an infinite loop, port PB0 will be on HIGH and port PB2 on LOW for 1 second, then port PB0 will be on LOW and port PB2 on HIGH for 1 second and the cycle will continue.

Image5- Code in C

Programming in the Arduino IDE

The configuration of the Arduino IDE to program the ATtiny45 was done in the Electronics Design week, I will go straight to the programming. In this program the serial was added using the include , it allows me to be able to read and write things in the serial. A button was added on the PB2 port as an input and the LED on the PB1 port as an output, the serial was started with a communication speed of 115200.

The program is quite simple, when the button is pressed it is at a high level, the program then enters the first “If” and makes the LED light up and writes the text “button pushed” in the IDE’s serial. When the button is not pressed, it enters in “Else”, write the text “button not pushed” in the IDE’s serial and the LED is off.

Image6- Arduino IDE

Group Assignment

Programming in Arduino uno

Configuration used for Arduino uno:

  • Board: Arduino Uno.
  • Programmer: AVRISP mkll.

Image7- AVRISP mkll

Programming

In this test, analog values were read from a joystick and transferred to a servo motor. First, I inserted the include “servo.h” to use the servo, then the analog port of the joystick was read and in the loop the association of the angulation from 0 to 180 is made with the analog port variation of 0. After having once the relationship is made, the position is passed on to the servo, with which the servo follows the position of the joystick.

Image8- Servo Code

In this experiment it is possible to change the angulation of the servomotor using the potentiometer.

Image9- Servo test

Programming the ESP8266 in the Arduino IDE

Configuration Used for ESP8266:

  • Select preferences in files.

Image10- Select Preferences

  • Added the ESP32 http://arduino.esp8266.com/stable/package_esp8266com_index.json

Image11- Add the esp32 URLS

  • In tools, the card manager is selected.

Image12 - card manager selecting

  • In the card manager after search for esp8266 click on install.

Image13 - Install esp8266

  • On tools and boards select the esp8266 model.

Image14 - esp8266 model select

  • After configuring the IDE for ESP8266, I selected the ESP8266 WifiClient example in examples in the files section.

Image15- select Wi-Fi config

  • This example will establish a TCP connection for a “quote of the day” service. He sends a “hello” message and then prints the data received, it is only necessary to modify the ssid and password to make the connection. The Arduino serial displays the connection feedback.

Image16- esp8266

Image17- TCP connection

Image18 - comparison between ESP32, ESP8266 and Arduino UNO

Pushing a button with serial

Update In week 14 I develop a new ESP PCB, and I did the same test and i got the same results.

Below information of the new PCB!!

This time, it was decided to make a general board using an ESP-WROOM-32 chip, for that the circuit provided by Neil in “embedded programming” was used as a basis.

  • ESP32
    • ESP32-WROOM-32
      • hello.ESP32-WROOM board components traces holes interior
        • echo.ino video ring.ino
        • Python video

Image19 - Board and components

For this, the components were first selected, and the schematic was done in the Proteus ISIS interface.

Image20 - Proteus schematic

Then, having done the schematic, the next step is to create the board design, this time, to facilitate the welding, it was chosen to use a 0.5mm separation between tracks to then create the SVG file to be used later in the mod.

Image21 - Design Layout

BOM (Bill of material):

  • 1x 30-pin Arduino female connectors (BRL 9.56)
  • 1x ESP-WROOM-32 chip (BRL 28.40)
  • 1x 3v3 Voltage Regulator (BRL 1.28)
  • 1x 0.1uF SMD capacitor (BRL 0.95)
  • 1x 1uF SMD capacitor (BRL 0.95)
  • 1x 10uF SMD capacitor (BRL 0.95)
  • 1x 10k ohm resistor (BRL 0.05)
  • 1x touch key (BRL 0.11)
  • 1x 3-state key (BRL 4.90)

Once the design was done, the RML files were created to be used in the Monofab SRM-20 machine and produce the desired board.

Image22 - Edge RML file

Image23 -Holes RML file

Image24 - Traces RML file

Then I prepared the PCB in the SRM machine and soldered the components

Image25 - PCB soldered

Conclusion

The ESP32 is the most complete in terms of communication possibilities, with native Wi-fi and Bluetooth, and is more powerful, due to its more powerful hardware. As it has Wi-fi on the board it is easier to carry out projects, as it does not need any accessory to make the connection, in addition to having an easy programming using the Arduino IDE.

  • All the files done in this week assignment can be find in the repository

Files

Disclaimer: due to pandemic and lockdowns I had limited lab access and resources to finish the assigment but I will get back to making my own board after I get ESP microchips and have normal acess to the lab.