Thursday, January 7, 2021

ESP32 (Arduino) ADC (Analog to Digital Converter), analogRead() and plot on 2.4" 320X240 Display (SPI ILI9341).

Example run on NodeMCU-32S, analogRead() from GPIO36, and plot on 2.4" 320X240 Display (SPI ILI9341).

To setup of the display, refer last post "ESP32 + 2.4" 320X240 Display (SPI ILI9341), using TFT_eSPI, prepare user setup file".


ESP32_ILI9341_AIN_20210108a.ino
/*
 * ESP32 + SPI ILI9341
 * Read Analog In and
 * plot on 2.4" 320x240 SPI ILI9341 screen
 */

#include "SPI.h"
#include "TFT_eSPI.h"

#define TFT_GREY 0x7BEF

TFT_eSPI myGLCD = TFT_eSPI();

#define FRAME_TOPX    10
#define FRAME_TOPY    5
#define FRAME_WIDTH   300
#define FRAME_HEIGHT  200
#define FRAME_BOTTOMY FRAME_TOPY + FRAME_HEIGHT

#define PinAnalogIn 36
int idx = 0;
#define IDX_MAX     300

void setup(){
  Serial.begin(115200);
  myGLCD.init();
  myGLCD.setRotation(1);
  myGLCD.fillScreen(TFT_BLACK);
  delay(500);
  myGLCD.fillRect(FRAME_TOPX-1, FRAME_TOPY-1, 
                  FRAME_WIDTH+2, FRAME_HEIGHT+2, TFT_WHITE);
  myGLCD.fillRect(FRAME_TOPX, FRAME_TOPY, 
                  FRAME_WIDTH, FRAME_HEIGHT, TFT_GREY);

  myGLCD.setTextColor(TFT_WHITE,TFT_BLACK);
  myGLCD.drawString("ESP32 (Arduino framework) + SPI ILI9341", 
                  FRAME_TOPX, FRAME_BOTTOMY+10);
  myGLCD.drawString("ADC exercise", FRAME_TOPX, FRAME_BOTTOMY+20);

  Serial.println("\n --- Start ---\n");
}

void loop(){
  int val = analogRead(PinAnalogIn);
  int AnalogInVal = map(val, 0, 4096, 0, 200);

  Serial.print(val);
  Serial.print(" : ");
  Serial.println(AnalogInVal);
  
  myGLCD.drawLine(
      FRAME_TOPX+idx, FRAME_BOTTOMY, 
      FRAME_TOPX+idx, FRAME_BOTTOMY-AnalogInVal, 
      TFT_WHITE);
  
  //Serial.println(hall);
  delay(100);
  
  idx++;
  if(idx >= IDX_MAX){
    idx = 0;
    myGLCD.fillRect(FRAME_TOPX, FRAME_TOPY, 
                    FRAME_WIDTH, FRAME_HEIGHT, TFT_GREY);
  }

}

Next:

No comments:

Post a Comment