Olenka Odar - Fab Academy

05 Embedded Programming

In this section we list the activities to be solved in the following table:

Checklist Estatus
Linked to the group assignment page Finished
Documented what you learned from browsing through a microcontroller datasheet. Finished
Described the programming process(es) you used Finished
Included your source code Finished
Included ‘hero shot(s)’ Finished

Group Assignment:

  • Compare the performance and development workflows for other architectures
  • Document your work to the group work page and reflect on your individual page what you learned

I will also do this assignment individually.

For this assignment I will work with the ATMEGA328P microcontroller that we can find in an ARDUINO UNO and with the ESP8266 microcontroller that is the basis of the WEMOS D1 ESP8266 board. Let's learn more about both microcontrollers.

ATMEGA328P - ARDUINO UNO

Features Programming environment

To program the Arduino Uno card we must first install the Arduino IDE, I leave you here the download link is very easy to install. The open-source Arduino Software (IDE) makes it easy to write code and upload it to the board. This software can be used with any Arduino board. The latest version is Arduino IDE 2.0.3. I have version 1.8.14 installed.

ESP8266 - WEMOS D1 ESP8266

Features

Programming environment

Wemos D1 can be programmed using the Arduino IDE. In this way, we can take advantage of the code made for arduino and take it to this card easily. To work with the WEMOS D1 ESP8266 card with the Arduino IDE, we must first configure and install the necessary libraries and drivers so that our card can be recognized and programmed. I based it on a YouTube video, he left the link here.

After downloading and decompressing the files, we proceed to install the driver.

Now we enter, from the Arduino IDE, the link in the card additional URLs manager.

We install the ESP8266 card library.

Programming in Arduino Software

I programmed both cards with the same "BLINK" program, which we can find in the examples provided by the Arduino software (we must take into account that when selecting ESP8266 we must choose the examples for that card). When downloading the programs the following appears:

WEMOS D1 ESP8266:Testing the Wi-Fi module

As this card is known to integrate an ESP8266 Wi-Fi module, I did not want to miss the opportunity to test the Wi-Fi module. Searching the internet I found a program that allows you to control the on and off of a led over the internet, based on that I made my program.

I leave the original code here. This program was used with a NodeMCU card with ESP-M2 wifi module, based on the ESP8285 SoC according to this video; but configuring by software it can be used with the WEMOS D1 ESP8266 card.

Once we load the program to the card, we enter the serial monitor to obtain information on the address of the internet page that we must access to control the on and off of the led, if the information does not appear, it is enough to press the button reset the card and the information will appear. Do not forget that it is necessary that we place the serial communication at 115200 baud.

I searched for the on/off images that appear on the internet and copied the address of the image, but since it was too long, I shortened the URL with the following online program "Acortar.link URL", and it worked correctly.

What I learned:

  • As I said at the beginning, I already have the Arduino IDE installed, when I installed the ESP8266 library, I installed version 3.1.1, when I wanted to compile a test program it gave me a compilation error and it did not allow me to download, I could not find the mistake!!! I reviewed the video in more detail and noticed that they worked with Arduino version 1.8.19, I have version 1.8.14 installed, I went back to the card manager and installed version 3.0.2 as a test (you can select the version to install) and with that version I worked without problems. We must be very careful when we install the libraries, since many times the compilation errors will depend on the version of the software and the installed library.
  • The 32-bit structure of the ESP8266 makes it faster than the Atmega328P which has an 8bit structure.
  • The ESP8266 features larger program storage and larger flash memory capacity.

Videos of what has been done:

Individual assignment 

  • Browse through the datasheet for your microcontroller
  • Program a microcontroller development board to interact and communicate

 This week I have to review the technical data sheets of microcontrollers from different families, let's see what we find.

Raspberry Pi Pico RP2040

This card is based on the RP2040 microcontroller which has a 32-bit structure. It is a low-cost, high-performance microcontroller board with flexible digital interfaces.

Features

Among its main features, it has an internal temperature sensor, it sounds interesting.

Programming environment

The Raspberry Pi PICO platform can be programmed with either MicroPython or C/C++, with MicroPython being the easiest way to get started. It is also possible to program the PICO from the Arduino IDE. I decided to work with MicroPython.

According to what I could read on the internet I need to install MicroPython firmware for Raspberry Pi Pico and the Thonny Python IDE to program the Raspberry Pi PICO RP 2040 board.

Downloading ThonnyInstalling ThonnyProgramming environment of the Thonny IDE

Installing MicroPython firmware for Raspberry Pi Pico

Checking connection

Programming the Raspberry Pi PICO RP 2040

The Raspberry Pi PICO RP 2040 among its features presents an internal temperature sensor. To program it using this sensor I used the formula found in your data sheet, keeping in mind that setting an AINSEL of 4 selects the internal temperature probe.

ARDUINO MEGA 2560

Board based on the ATmega2560 microcontroller with an 8-bit structure. Let's see some of its features.

In its data sheet it indicates that it has two pins for the 2-wire serial interface, I will use these pins to connect a 0.96'' I2C 128*64 SSD1306 OLED screen.

PIC 16F876A

In my student days, PIC microcontrollers were the most used microcontrollers; with its CPU architecture RISC (Reduced Instruction Set Computer), set of 35 instructions. PICs are very versatile since they contain all the components to perform and control a task.

Main features:

This is my trainer board made with the 16F876A microcontroller.

Programming the PIC 16F876A:

To program the PIC 16F876A, I used the PIC C COMPILER software to make the program and the PICkit 2 Programmer software needed to write a program on a PIC using the PICkit2 programmer.

This PIC has certain pins that allow it to be programmed through an ICSP (In Circuit Serial Programming) connection. This technology included in all PIC microcontrollers makes it possible to reprogram them without it being necessary to remove them from their application circuit to take them to the programmer. The signals connected between the PIC and the programming device to write, read and verify the program are the following:

We open the program, click on File – New – Source File; We put the name "program" and click on save. We proceed to carry out the program in the PIC C Compiler environment, as shown below:

Do not forget: when programming a PIC, to declare a pin as an input the corresponding bit must be set to 1 and to declare a pin as an output the corresponding bit must be set to 0.

Finally we compile the program and if there are no errors we are ready to save the PIC. When compiling the program, a file is created, CCS program with HEX extension, which must be imported from the PICkit 2 Programmer software to record the PIC. We connect the programmer through the USB port and proceed to record the PIC.

 ATtiny85

The Attiny85 is a low-power, high-performance 8-bit AVR microcontroller, its main features are described below:

  • 8-pin package
  • 8KB Flash Memory
  • 512B EEPROM
  • 512B SRAM
  • Operating voltage: 2.7 – 5.5V
  • Maximum operating frequency: 20 MHz
  • Digital Communication Peripherals: 1 - SPI, 1 - I2C
  • 8-bit Timer/Counter with Prescaler and Two PWM Channel
  • Features 6 digital I/O pins that can be configured with internal pull-up resistor: PB0, PB1, PB2, PB3, PB4, PB5
  • 4 channels of 10-bit ADC: ADC0, ADC1, ADC2, ADC3

To program this microcontroller I will use the SPI interface (Serial Peripheral Interface) on the MOSI (PB0), MISO (PB1), SCK (PB2), RESET (PB5) pins, as detailed in its PINOUT.

PWM: I will place a led on physical pin 3 (PB4) which, according to its datasheet, has a PWM output that will allow me to regulate its brightness. And on physical pin 2 (PB3) a button.

I don't have this microcontroller on any board, but I still wanted to program an ATtiny85 microcontroller, so I used an Arduino Uno board and an ATtiny85 microcontroller with DIP package.

Programming of the ATtiny85

Arduino board as a programmer

Next, I show an image of the pinout of the Arduino Uno based on the ATMEGA328P microcontroller, giving importance to the following physical pins that will allow me to program the ATtiny85 by SPI:

To carry out the programming, we must first convert the Arduino Uno board into an ISP programmer, for this we go to File and in "Examples" select "Arduino ISP". We connect our card, enter "Tools" and verify that it is configured as follows:

Compile and download the “Arduino ISP” program to the Arduino Uno board and you are ready to program the ATtiny85.

Configuring the Arduino IDE to program ATTiny

In order to program the ATtiny85 microcontroller with the Arduino IDE, it is necessary to install the corresponding library, as indicated below:

  • We select «Files/Preferences» in the Arduino IDE.
  • We find the field “Additional card URLs manager”. Paste the following URL into the field (we'll use a comma if there's any other URL in the list): "https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json" , then click OK to save.
  • Then select Tools/Plate/Card Manager and write ATtiny in the search field. Then we click on ATTiny by David A. Mellis and select install.
  • Once the installation is finished, we select close.
  • Now we can choose the board by entering Tools/Boards/ATtiny Microcontrollers and selecting ATtiny25/45/85.
  • Then entering Tools / Processor we choose "ATtiny85".
  • Finally go to Tools and in Clock select the internal clock frequency 1MHz (you can also work with 16MHz)
  • Now we proceed to write our program.
  • Important: before downloading the program, check the tools; motherboard, processor and clock, should appear as shown in the figure; also in "Programmer" we must choose the option "Arduino as ISP" and that's it; now we can compile and download the program to the ATtiny85 microcontroller without any problem.
  • Programming ATTINY85
  • Checking the program's operation

What I learned:

  • For the ADC conversion factor in the Raspberry Pi PICO RP2040, the following must be taken into account:
  • The ATMEGA2560 board has larger flash memory and program storage than an ARDUINO UNO board.
  • Another advantage that the ATMEGA2560 card has over the ARDUINO UNO is that it has dedicated pins exclusively for I2C communication, that is, they do not share digital or analog pin function.
  • Programming a PIC through ICSP communication has a great advantage: it makes the task of programming it faster and prevents the pins of the PIC from bending and breaking by not having to remove it from its application circuit and take it to the programmer.
  • The ATTINY85 microcontroller can be used for small projects due to its reduced amount of memory and inputs and outputs. By presenting its multifunction pins, they can be programmed through SPI communication with an ARDUINO UNO and using the same Arduino IDE.
  • To make the most of our microcontroller, we must always refer to its data sheet, in this way we will make correct use of its pins and the functions it presents. 

Programs

//WEMOS D1 ESP8266
//ARDUINO IDE 

#include <ESP8266WiFi.h>
const char* ssid = "YENQUEODAR-1";
const char* password = "Chiclayo.22";
WiFiServer server(80);

int estado = LOW;

void setup() {
Serial.begin(115200);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);

WiFi.begin(ssid, password);
Serial.printf("\n\nConectando a la red: %s\n", WiFi.SSID().c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectada");
server.begin();
Serial.println("Servidor inicializado");
Serial.printf("\n\nUtiliza esta URL para conectar: http://%s/\n", WiFi.localIP().toString().c_str());
}

void loop()
{

WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("nuevo cliente");
while(!client.available()){
delay(1);
}
String peticion = client.readStringUntil('\r');
Serial.println(peticion);
client.flush();

if (peticion.indexOf('/LED=ON') != -1)
{estado = LOW;}
if (peticion.indexOf('/LED=OFF') != -1)
{estado = HIGH;}

digitalWrite(LED_BUILTIN, estado);

client.println("HTTP/1.1 200 OK");
client.println("");
client.println("");
client.println("");
client.println("");

client.print("<h1 align=center>CONTROL LED ");
//client.print("<h1 align=center>CONTROL LED33 ");

if(estado == HIGH)
{client.print("<input type='image' src='https://acortar.link/xx1Fss' style='display:block; margin:auto' width='30%' onClick=location.href='/LED=ON'>");
client.print("<h1 align=center>LED OFF ");
client.print("<h1 align=center> WAITING TO TURN ON LED");}
else
{client.print("<input type='image' src='https://acortar.link/dImuGO' style='display:block; margin:auto' width='20%' onClick=location.href='/LED=OFF'>");
client.print("<h1 align=center>LED ON ");
client.print("<h1 align=center>WAITING FOR LED TURN OFF ");}

client.println("</html>");
delay(1);
Serial.println("Peticion finalizada"); // Se finaliza la petición al cliente. Se inicializa la espera de una nueva petición.
Serial.println("");
}

#RASPBERRY PI PICO RP2040
#​MicroPython 

import machine
import time
sensor = machine.ADC(4) # selection of the internal temperature probe
factor = 3.3/65535 #conversion to 16 bits
while 1:    
    ADC_voltage = sensor.read_u16()*factor    
    Temperature = round (27 - (ADC_voltage - 0.706)/0.001721) #formula data sheet   
    print( ADC_voltage)   
    print("T°:", Temperature)   
    time.sleep_ms(1000)

//ATMEGA 2560
//ARDUINO IDE

//CONFIGURATION FOR OLED DISPLAY
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "stdio.h"

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

const int ancho = 16 ;
byte data ;
int valor;

void setup(){
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC); // Start display
display.clearDisplay(); // Erase everything
}

void loop(){
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(30,11);
display.println("OLENKA ODAR ");
display.setCursor(85,0);
display.println("2023");
display.setTextSize(1);
display.setCursor(0,0);
display.println("I2C");
display.setTextSize(1);
display.setCursor(31,21);
display.println("FAB ACADEMY");
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(33,10);
display.println("HELLO NEIL");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(20,20);
display.println("CHICLAYO - PERU");
display.display();
delay(2000);
}

//PIC 16F876A
//PIC C COMPILER
// PICKIT2

#BYTE PORTA = 05 //REGISTRATION ADDRESS
#BIT LED = PORTA.4

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits

#use delay(clock=20000000)

void main()
{
SET_TRIS_A(0); //RA AS OUTPUT FOR LED

WHILE(TRUE){
LED = 0; //LED OFF 
delay_ms(100); //DELAY 100ms
LED = 1; //LED ON 
delay_ms(1000);
}

//Olenka Odar - Fab Academy 2023
//ATtiny85 "BUTTON_LED" 
//ARDUINO IDE

int buttonstate=0;
int button5=0;
int pulso_sal=0;
int k=0;
void setup() {

pinMode(4,OUTPUT);
pinMode(3, INPUT);
}
void loop() {
while(true){

buttonstate= digitalRead(3);
if(buttonstate==HIGH)pulso_sal=1;
if(buttonstate==LOW&&pulso_sal==1){
pulso_sal=0;
button5= button5+1;}

if(button5>3)button5=0;

if(button5==0){
digitalWrite(4, LOW);
delay(10);
}
if(button5==1){
digitalWrite(4,HIGH);
delay(10);
}
if(button5==2){
digitalWrite(4,HIGH);
delay(100);
digitalWrite(4,LOW);
delay(100);
}
if(button5==3&&buttonstate==LOW){
for(int y=0; y<=155; y++){
for (k=0; k<100;k++){
analogWrite(4,y);}}
}
if(button5==3&&buttonstate==HIGH)button5=-1;
}
}

   4.   6 .