Introduction: Incredibly Easy to Program!

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects.

Today, I’ll talk about the STM32 Core, the L476RG, which is the face of Ultra Low Power. You can see it on the left of the image. This device has two female pin bars, one on each side, which are nothing more than the connectors for the arduino shield. This is great, no?

In my opinion, STMicroelectronics did this in its Development Kit because it knows professionals use this chip. This company is going more and more towards the arduino. And this is also true for several other professional STMicroelectronics kits.

Finally, concerning the project today, we will use two DS18b20 sensors in addition to the L476RG. So we’ll make a simple assembly using the L476RG, import a library into the MBED environment, create a program in the MBED environment, and get data from the L476RG via USB / Serial.

I have already talked a little about the L476RG in this video: THE EASIER WAY TO PROGRAM A MICROCONTROLLER, where I show how to configure the MBED environment, which is online.

Some people who follow my videos are asking me if STM32 replaces ESP32. I say one thing: it does not replace and it could not, because they are two completely different things.

This STM32 chip is a microcontroller, or rather; it is not a "cluster of things" as is the ESP32. So the name may look similar, but they’re entirely different. The STM32 is a general purpose microcontroller, such as a PIC, an Atmel, for example.

Step 1: Resources Used

1 Core L476RG

2 DS18b20 sensors (we use the common waterproof modules on the market)

1 4k7 resistor

Mini protoboard

Jumpers for connection

Step 2: Assembly

We will initially carry out the assembly using one of the temperature sensors.

Its power will be 5V.

A 4k7 resistor will be used to do a pull-up on the data line (1-Wire).

We will read the data using the A0 pin.

Step 3: New Program in MBED

Once you have your account set up in MBED and access it, we will create a new program. To do this, right click on "My Programs" and select "New Program ..."

Confirm that the "Platform" conforms to the board you are using.

We now click on "Template".

We’ll create a program based on the example, "Display a message on PC using UART".

Enter the name of the program in "Program Name".

Check the "Update this program and libraries to latest revision" option.

A new folder for your program will be created, including the default MBED library and the main.cpp file.

You can use it to test if everything is working well. To do this, simply compile it and copy it to the platform.

Using a serial terminal of your choice, you can receive the following messages.

Step 4: Importing the DS18b20 Library

As there are several versions of libraries for the Ds18b20, we will import using a url so that your example uses the same library.

Step 5: New Program in MBED

In the "Source URL" field fill in: https://os.mbed.com/users/Sissors/code/DS1820/ and click import.

Your DS1820 library should appear in your program folder.

Step 6: Source Code

Includes

We started by including the necessary libraries.

#include "mbed.h" //inclusão da biblioteca padrão do MBED
#include "DS1820.h" //inclusão da biblioteca do sensor DS1820

We define constants that will represent the pins used.

Note that the DS18b20 is a sensor with 1-WIRE communication. For this reason, we use the library that will handle the entire protocol of communication with the devices. This includes identifying each device up to the read commands.

#define PINO_DE_DADOS A0 //define o pino para leitura dos dados
#define MAX_SENSORES 16 //define o número máximo para o vetor de sensores

We create a vector that will point to each of the 16 possible devices connected to the data line.

DS1820* sensor[MAX_SENSORES]; //cria um vetor com 16 posições para os sensores

We start the main () method, where, using the "unassignedProbe ()" method contained in the DS1820 library, we look for all available devices in the communication line.

We fill the sensor vector [] with the instances that will represent each of the available sensors.

We do this until the last one is found or until we reach the maximum of 16 sensors.

int main()
{ int encontrados = 0; while(DS1820::unassignedProbe(PINO_DE_DADOS)) { //inicia a procura por sensores sensor[encontrados] = new DS1820(PINO_DE_DADOS); //cria uma instancia para o sensor encontrado encontrados++; if (encontrados == MAX_SENSORES) //verifica se atingiu o máximo de sensores break; }

We send the number of sensors found on the line.

printf("Dispositivos encontrado(s): %d \r\n\n", encontrados);

We start an infinite loop, requesting that all available sensors calculate their respective temperatures, and then iterate through the sensor vector by sending the readings obtained.

printf("Dispositivos encontrado(s): %d \r\n\n", encontrados);
while(1) { sensor[0]->convertTemperature(true, DS1820::all_devices); //solicita a leitura de temperatura para todos os dispositivos encontrados for (int i = 0; itemperature()); // . . . e retorna a temperatura printf("\r\n"); wait(1); }

Step 7: Data Received

Using a single sensor, we obtain the following serial output.

Step 8: Including More Sensors

To test the code, we introduce another sensor in the communication line, simply by connecting it in parallel with the first sensor.

Remember to switch off the assembly before connecting new sensors.

When restarting the assembly, we obtained the following output, without any changes in the source code.

Step 9: View Source

#include "mbed.h" //inclusão da biblioteca padrão do MBED
#include "DS1820.h" //inclusão da biblioteca do sensor DS1820 #define PINO_DE_DADOS A0 //define o pino para leitura dos dados #define MAX_SENSORES 16 //define o número máximo para o vetor de sensores DS1820* sensor[MAX_SENSORES]; //cria um vetor com 16 posições para os sensores int main() { int encontrados = 0; while(DS1820::unassignedProbe(PINO_DE_DADOS)) { //inicia a procura por sensores sensor[encontrados] = new DS1820(PINO_DE_DADOS); //cria uma instancia para o sensor encontrado encontrados++; if (encontrados == MAX_SENSORES) //verifica se atingiu o máximo de sensores break; } printf("Dispositivos encontrado(s): %d \r\n\n", encontrados); while(1) { sensor[0]->convertTemperature(true, DS1820::all_devices); //solicita a leitura de temperatura para todos os dispositivos encontrados for (int i = 0; itemperature()); // . . . e retorna a temperatura printf("\r\n"); wait(1); } }

Step 10: Files