▷ Practice 37: Using two core of ESP-32

 ⭐⭐⭐⭐⭐ Practice 37: Using two core of ESP-32

General objective:

  • Multitask with the two cores of the ESP32.

Materials:

  • TSC-Lab

Introduction:

The ESP-32 has 2 32-bit Xtensa LX6 microprocessors called core 0 and core 1. By default, the code runs on core 1. However, in this laboratory practice, you will learn how to utilize the dual-core of the ESP-32 by performing multitasking, working with both the temperature plant and the motor speed simultaneously.

Steps:

Note: It is assumed that all libraries have been previously installed. 
1- Copy and upload the following code to the TSC-Lab. 
/*
****************************** TSC-Lab *******************************
***************************** PRACTICE 34 *****************************
This practice is about use of two core esp-32
By: Kevin E. Chica O
More information: https://tsc-lab.blogspot.com/
*/
//temperature
#include <OneWire.h>
#include <DallasTemperature.h>
// GPIO pin 0 is set as OneWire bus
OneWire ourWire1(0);
//GPIO pin 4 is set as OneWire bus
OneWire ourWire2(4);
//A variable or object is declared for our sensor 1
DallasTemperature sensors1(&ourWire1);
//A variable or object is declared for our sensor 2
DallasTemperature sensors2(&ourWire2);
//set parameters
int period_temp = 12; //medium period
int freq_temp = 2000; // sampling time
//motor
//initial setting for data acquisition
int dutyCycleInitial = 255;
int dutyCycleFinish = 0;
int period = 13000;
int cycles = 10;
//separador library
#include <Separador.h>
Separador s;
//motor
int motor1Pin1 = 33;
int motor1Pin2 = 25;
int enable1Pin = 32;
int motor_status = 255;
// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
//move
String move_motor = "counterclockwise";
int encoder = 27;
void motor( void *pvParameters );
void RPM( void *pvParameters );
void readTemperature( void *pvParameters );
volatile int counter = 0;
void interruption() // Function that runs during each interrupt
{
counter++;
}
void setup() {
Serial.begin(115200);
//begin temperature sensor
sensors1.begin(); //Sensor 1 starts
sensors2.begin(); //Sensor 2 starts
// sets the pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
// configure LED PWM functionalitites
ledcSetup(pwmChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(enable1Pin, pwmChannel);
attachInterrupt(encoder, interruption, RISING);
xTaskCreatePinnedToCore(
motor
, "MotorDC" // Descriptive name of the function (MAX 8 characters)
, 2048 // Size required in STACK memory
, NULL // INITIAL parameter to receive (void *)
, 1 // Priority, priority = 3 (configMAX_PRIORITIES - 1) is the highest, priority = 0 is the lowest.
, NULL // Variable that points to the task (optional)
, 1); // core 1
xTaskCreatePinnedToCore(
RPM
, "RPM" // Descriptive name of the function (MAX 8 characters)
, 2048 // Size required in STACK memory
, NULL // INITIAL parameter to receive (void *)
, 1 // Priority, priority = 3 (configMAX_PRIORITIES - 1) is the highest, priority = 0 is the lowest.
, NULL // Variable that points to the task (optional)
, 1); // core 1
xTaskCreatePinnedToCore(
readTemperature
, "rTemp" // Descriptive name of the function (MAX 8 characters)
, 2048 // Size required in STACK memory
, NULL // INITIAL parameter to receive (void *)
, 1 // Priority, priority = 3 (configMAX_PRIORITIES - 1) is the highest, priority = 0 is the lowest.
, NULL // Variable that points to the task (optional)
, 0); // core 0
}
void loop() {
}
void motor( void *pvParameters ) {
while (1) {
if (cycles > 0) {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
ledcWrite(pwmChannel, dutyCycleInitial);
motor_status = 255;
vTaskDelay(period);
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
ledcWrite(pwmChannel, dutyCycleFinish);
motor_status = 0;
vTaskDelay(period);
cycles--;
}
}
}
void RPM( void *pvParameters ) {
while (1) {
vTaskDelay(999);
Serial.print(counter * 60);
Serial.print(",");
Serial.println(motor_status);
counter = 0;
}
}
void readTemperature( void *pvParameters ) {
while (1) {
uint32_t timer = period_temp * 60000L;
for ( uint32_t tStart = millis(); (millis() - tStart) < timer; ) {
//The command is sent to read the temperature
sensors1.requestTemperatures();
//Obtain the temperature in ºC of sensor 1
float temp1 = sensors1.getTempCByIndex(0);
//The command is sent to read the temperature
sensors2.requestTemperatures();
//Obtain the temperature in ºC of sensor 2
float temp2 = sensors2.getTempCByIndex(0);
//print to display the temperature change
Serial.println();
Serial.print(temp1);
Serial.print(",");
Serial.println(temp2);
delay(freq_temp);
}
}
}
 



2- In the serial monitor, you will see something similar to this:


                  

Read related topics

Comentarios

Popular Posts

▷ Especificaciones del módulo ESP32

▷ #ESP32 - REAL-TIME CLOCK #RTC INTERNO

▷ #ESP32 - SINCRONIZAR RTC INTERNO CON SERVIDOR NTP

▷ #ESP32 - Display OLED 128x64

▷ Sensor networks for agriculture based on #FPGA

▷ #ESP32 - INSTALAR LIBRERIAS DESDE GESTOR LIBRERIAS ARDUINO

▷ Instalación de ALTIUM CircuitMaker y especificaciones del módulo ESP32

▷ SISTEMAS EMBEBIDOS, PROYECTOS PROPUESTOS (2021 PAO1)

▷ #ESP32 - Over-The-Air programming #OTA

▷ DISEÑO DE SISTEMAS DIGITALES, PROYECTOS PROPUESTOS (2019 2do Término)