Esp32 Instalação e Exemplos
Esp32 Instalação e Exemplos
The ESP32 is an under US$10 board with great advantages over similar IoT boards in the market.
This board has a dual processed microprocessor that helps a lot, because when one processor is
handle communication, the other one is in charge of I/O control, for example. This feature will
prevent some issues that happen with ESP8266, where the sole CPU needs stop controlling I/Os
when handle with Comm. Besides, the ESP32 has integrated WIFI, BLUETOOTH, DAC, several
ADC (not only one as the ESP8266), capacitive touch sensors, etc (give a look at above block
diagram). And the good news is that Power Consumption is almost the same as ESP8266.
Bellow a chart that can show us its main characteristics, and differences when compared with
ESP8266:
Let’s point its main properties for more details:
Key Features:
• 240 MHz dual-core Tensilica LX6 microcontroller with 600 DMIPS
• Integrated 520 KB SRAM
• Integrated 802.11 b/g/n HT40 Wi-Fi transceiver, baseband, stack and LwIP
• Integrated dual mode Bluetooth (classic and BLE)
• 16 MB flash, memory-mapped to the CPU code space
• 2.3V to 3.6V operating voltage
• -40°C to +125°C operating temperature
• Onboard PCB antenna / IPEX connector for external antenna
Sensors:
• Ultra-low noise analog amplifier
• Hall sensor
• 10x capacitive touch interfaces
• 32 kHz crystal oscillator
34 x GPIO:
• 3 x UARTs, including hardware flow control
• 3 x SPI
• 2 x I2S
• 18 x ADC input channels
• 2 x DAC
• 2 x I2C
• PWM/timer input/output available on every GPIO pin
• OpenOCD debug interface with 32 kB TRAX buffer
• SDIO master/slave 50 MHz
• Supports external SPI flash up to 16 MB
• SD-card interface support
Security Related:
• WEP, WPA/WPA2 PSK/Enterprise
• Hardware accelerated encryption: AES/SHA2/Elliptical Curve Cryptography/RSA-4096
Performance:
• Supports sniffer, Station, SoftAP and Wi-Fi direct mode
• Max data rate of 150 Mbps@11n HT40, 72 Mbps@11n HT20, 54 Mbps@11g, and 11
Mbps@11b
• Maximum transmit power of 19.5 dBm@11b, 16.5 dBm@11g, 15.5 dBm@11n
• Minimum receiver sensitivity of -97 dBm
• 135 Mbps UDP sustained throughput
• 5 μA power consumption in Deep-sleep
• Arduino IDE
• MicroPython
Micropython: https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/
Pinout
Step 1: Open Terminal and execute the following command (copy->paste and hit
enter):
Step 1.1: Allow non root user to use tty0 (USB to Serial converter) serial communication with
ESP32
sudo usermod -a -G dialout $USER
Step 1.2: Install git. By default its installed with ubuntu linux installation
sudo apt-get install git
Step 1.3: Get repository for get–pip.py, is a bootstrapping script that enables users to install pip,
setup tools, and wheel in Python environments that don’t already have them.
wget https://bootstrap.pypa.io/get-pip.py
Step 1.4: Run python get-pip.py
sudo python get-pip.py
Step 1.5: Install pySerial is a Python API module to access the serial port. pySerial provides a
uniform API across multiple operating systems, including Windows, Linux, and BSD.
sudo pip3 install pyserial
Step 1.6: On terminal go to you Arduino installation directory using cd command
Após a ligação do NodeMCU-32S no computador ele será identificado e a busca por drivers de
instalação vai iniciar. Por padrão, o Windows vai procurar pelos drivers na internet e se ele
encontrar a instalação vai ocorrer de forma automática.
Caso o Windows não consiga encontrar os drivers para instalação da placa, então terá que instalar
manualmente.
Faça download do driver no link abaixo:
Driver CP2102 para Windows
Após o download faça a instalação dos drivers. Terminado a instalação, seu NodeMCU-32S já está
pronto para ser usado.
Após inserir as informações, clique em “Clone” para que o processo de cópia do repositório seja
realizado:
Este processo é um pouco demorado, portanto paciência.
Finalizado a clonagem do repositório, acesse o caminho “C:/Users/[ USUARIO-
PC]/Documents/Arduino/hardware/espressif/esp32/tools” (lembre-se de mudar [USUARIO-PC]
para o nome de utilizador do seu computador) e execute o “get.exe” e aguarde:
Ainda no menu “Ferramentas”, selecione a opção porta e marque a porta COM em que sua placa foi
alocada:
Veja que a minha placa foi alocada na COM3, porém, o seu NodemCU-32S pode ter sido alocado
em uma COM de outro valor. Caso não saiba em qual porta COM sua placa foi alocada, basta
retornar no menu Iniciar do Windows, aceder a opção Dispositivos e Impressoras e verificar a porta
em que seu NodeMCU-32S está ligado e retornar na IDE e selecionar a porta COM.
Feito essas configurações, sua IDE está pronta para enviar os códigos ao NodeMCU-32S.
Para um teste rápido, na IDE clique no menu “Ficheiro”, selecione a opção “Exemplos”, em seguida
“01.Basics” e selecione “Blink”. Uma nova janela da IDE vai abrir. Faça a conferência das
informações para garantir que nada foi alterado na placa e na porta. Se atente ao rodapé da IDE que
mostra a placa e a porta COM que está configurada na IDE:
Agora, clique no botão de upload do código para o NodeMCU-32S e aguarde o código ser
carregado na placa.
Terminado o carregamento, o LED da placa irá piscar com intervalo de 1 segundo:
Sempre que abrir a IDE, faça a conferência das informações para garantir que nada foi alterado.
Geralmente, feito a configurações a primeira vez ela vai se manter sem necessidade de fazer
alteração.
Reforçando: sem executar os passos de definição de placa e porta, caso você escreva o código ou
utilize algum exemplo de código e tente carregar no NodeMCU-32S, o carregamento do código
para a placa não será bem sucedido e um erro será mostrado na IDE.
Exemplos de Programas
A hall effect sensor can detect variations in the magnetic field in its surroundings. The greater the
magnetic field, the greater the sensor’s output voltage.
The hall effect sensor can be combined with a threshold detection to act as a switch, for example.
Additionally, hall effect sensors are mainly used to:
• Detect proximity;
• Calculate positioning;
• Count the number of revolutions of a wheel;
• Detect a door closing;
• And much more.
int val = 0;
void setup() {
Serial.begin(9600);
}
This example simply reads the hall sensor measurements and displays them on the Serial monitor.
val = hallRead();
Serial.println(val);
Add a delay of one second in the loop, so that you can actually read the values.
delay(1000);
void setup()
{
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Touch Test");
pinMode(LED_PIN, OUTPUT);
digitalWrite (LED_PIN, LOW);
}
void loop()
{
touch_value = touchRead(TOUTCH_PIN);
Serial.println(touch_value); // get value using T0
if (touch_value < 50)
{
digitalWrite (LED_PIN, HIGH);
}
else
{
digitalWrite (LED_PIN, LOW);
}
delay(1000);
}
touch.ino Mjrovai
Em vazio: >50 e led desligado
Em contacto: <10 e led ligado
Exemplo 3: Entrada Analógica
GPIO ADC Channel
• GPIO 0 ==> ADC2_CH1
• GPIO 2 ==> ADC2_CH2
• GPIO 4 ==> ADC2_CH0
• GPIO 12 => ADC2_CH5
• GPIO 13 => ADC2_CH4
• GPIO 14 => ADC2_CH6
• GPIO 15 => ADC2_CH3
• GPIO 25 => ADC2_CH8
• GPIO 26 => ADC2_CH9
• GPIO 27 => ADC2_CH7
• GPIO 32 => ADC1_CH4
• GPIO 33 => ADC1_CH5
• GPIO 34 => ADC1_CH6
• GPIO 35 => ADC1_CH7
• GPIO 36 => ADC1_CH0
• GPIO 37 => ADC1_CH1
• GPIO 38 => ADC1_CH2
• GPIO 39 => ADC1_CH3
/******************************************************
* ESP32 Analog Input Test
* Analog Input: ADC_1_0 pin ==> GPIO36 (VP).
*
* MJRoBot.org 6Sept17
*****************************************************/
//Analog Input
#define ANALOG_PIN_0 36
int analog_value = 0;
void setup()
{
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Analog IN Test");
}
void loop()
{
analog_value = analogRead(ANALOG_PIN_0);
Serial.println(analog_value);
delay(500);
}
RTD
Com a RTD é possível ir dos 0 (luz total – telemóvel em cima sensor) aos 4095 (escuridão total –
dedo no sensor)
analog.ino
Exemplo 4: Entrada Analógica + Saída PWD
//Analog Input
#define ANALOG_PIN_0 36
int analog_value = 0;
// PMW LED
#define LED_PIN 2
int freq = 5000;
int ledChannel = 0;
int resolution = 8;
int dutyCycle = 0;
void setup()
{
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Analog IN/OUT Test");
analog_PWM.ino