Measure Distance With Maker Nano RP2040 And Ultrasonic Sensor With TFT Display

Measure Distance With Maker Nano RP2040 And Ultrasonic Sensor With TFT Display

 

Introduction

In this tutorial we shall see how to interface Maker Nano RP2040 with HC-SR04P and TFT 1.44". Ultimately to measure the distance and display it on the tft display.

 

Video

Hardware Preparation

This is the list of items used in the video.

 

Building The Circuit

  1. The circuit for this tutorial is constructed on a breadboard with jumper wires. The connection between the Maker Nano RP2040 and Ultrasonic Sensor HC-SR04P and Maker Nano RP2040 with 1.44' TFT display is as shown in below figure

 

connection

Once the connection are made as per the figure above, we can now start to code.

 

Code

The code for this tutorial is as shown below. Make sure to download the necessary library from the library manager. The "Ultrasonic.h" library can be downloaded from the Cytron GitHub page.

#include     // Core graphics library
#include  // Hardware-specific library for ST7735
#include  // Hardware-specific library for ST7789
#include 
#include 
#include   
#include "Ultrasonic.h"

Ultrasonic ultrasonic(2,3);

#define TFT_CS        10
#define TFT_RST        7 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC         9

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

int x;
void setup() {
  // put your setup code here, to run once:
  
  tft.initR(INITR_144GREENTAB); // Init ST7735R chip, green tab
  tft.fillScreen(ST77XX_BLACK);
  tft.drawRect(0, 0, 128, 128, ST7735_WHITE);
  tft.drawRect(1, 1, 126, 126, ST7735_WHITE);

  welcomeMessage();
  sensor();

}

void loop( ) {
  // put your main code here, to run repeatedly:
   tft.setCursor(40, 90);
   tft.setTextColor(ST77XX_BLACK);
   tft.setFont(&FreeMonoBold18pt7b);
   tft.fillRect(32, 60, 50, 100, ST7735_WHITE);
   tft.println(ultrasonic.Ranging(CM));
   tft.setCursor(40, 120);
   tft.println("CM");
   delay(100);
}


void welcomeMessage() {
  
  tft.setTextWrap(false);
  tft.fillScreen(ST77XX_BLACK);
  tft.setCursor(7, 10);
  tft.setTextColor(ST77XX_RED);
  tft.setFont(&FreeMonoBold9pt7b);
  tft.setTextSize(1);
  tft.println("MAKER NANO");
  tft.setCursor(30, 30);
  tft.println("RP2040");
  
  tft.setCursor(9, 45);
  tft.println("ULTRASONIC");
  tft.setCursor(4, 60);
  tft.println("SENSOR WITH");
  tft.setCursor(5, 75);
  tft.println("TFT DISPLAY");

  tft.drawRect(0, 0, 128, 128, ST7735_WHITE);
  tft.drawRect(1, 1, 126, 126, ST7735_WHITE);

  tft.drawRect(8, 90, 105, 11, ST7735_WHITE);
  for(x=0;x<110;x=x+10){
  tft.fillRect(10, 92, x, 7, ST7735_BLUE);
  tft.setCursor(30, 115);
  tft.fillRect(10, 104, 40, 20, ST7735_BLACK);
  tft.setTextColor(ST77XX_GREEN);
  tft.println(x);
  tft.setCursor(60, 115);
  tft.println("%");
  delay(800);
  
  }
}

void sensor(){
    tft.fillScreen(ST77XX_WHITE);
    tft.drawRect(0, 0, 128, 128, ST7735_YELLOW);
    tft.drawRect(1, 1, 126, 126, ST7735_YELLOW);
    tft.setTextColor(ST77XX_BLACK);
    tft.setFont(&FreeMonoBold9pt7b);
    tft.setCursor(5, 30);
    tft.println("DISTANCE :");
}
 

Outcome

 

dcim101mediadji 0088.jpg

 

Thank You

Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

"Please be reminded, this tutorial is prepared for you to try and learn.
You are encouraged to improve the code for a better application."




Chat hỗ trợ
Chat ngay