Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Amaan Javed
Published

M5Stack AREA AND DISTANCE CALCULATOR

In this project, I'm using an ultrasonic sensor to measure distance and calculate the area of a circle and square and display it on screen

BeginnerFull instructions provided1 hour2,194
M5Stack AREA AND DISTANCE CALCULATOR

Things used in this project

Hardware components

ESP32 Basic Core IoT Development Kit
M5Stack ESP32 Basic Core IoT Development Kit
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Male-Header 5 Position- 1 Row- Long (0.1")
Male-Header 5 Position- 1 Row- Long (0.1")
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering Station, 110 V
Soldering Station, 110 V

Story

Read more

Code

M5Stack Area

Arduino
#include<M5Stack.h>
const int trigPin = 22;
const int echoPin = 21;
long dur;
float dis;
float c;
float s;
void setup() {
  M5.begin();
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);// put your setup code here, to run once:
  M5.Lcd.fillScreen(BLUE);
  M5.Lcd.setTextColor(WHITE);
  M5.Lcd.setTextSize(3);
  M5.Lcd.setCursor(65, 10);
  M5.Lcd.print("AREA CAL: ");
  M5.Lcd.setCursor(3, 35);
  M5.Lcd.println("A FOR SQ:");
  M5.Lcd.println("B FOR CR:");
  M5.Lcd.println("C FOR DIS:");
}

void loop() {
  M5.update();
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  dur = pulseIn(echoPin, HIGH);
  dis= dur*0.034/2;
 
  if (M5.BtnA.wasReleased()) {
     s = dis*dis;
     M5.Lcd.clear(BLUE);
     M5.Lcd.setTextColor(WHITE);
     M5.Lcd.setTextSize(3);
     M5.Lcd.setCursor(65, 10);
     M5.Lcd.print("AREA CAL: ");
     M5.Lcd.setCursor(3, 35);
     M5.Lcd.println("Area(cm):");
     M5.Lcd.setTextSize(9);
     M5.Lcd.setCursor(140, 70);
     M5.Lcd.print(s);// leave
  }
  if (M5.BtnB.wasReleased()) {
     c = ((dis/2)^2)*PI;
     M5.Lcd.clear(BLUE);
     M5.Lcd.setTextColor(WHITE);
     M5.Lcd.setTextSize(3);
     M5.Lcd.setCursor(65, 10);
     M5.Lcd.print("AREA CAL: ");
     M5.Lcd.setCursor(3, 35);
     M5.Lcd.println("Area(cm):");
     M5.Lcd.setTextSize(9);
     M5.Lcd.setCursor(140, 70);
     M5.Lcd.print(c);// leave
  }
  if (M5.BtnC.wasReleased()) {
     M5.Lcd.clear(BLUE);
     M5.Lcd.setTextColor(WHITE);
     M5.Lcd.setTextSize(3);
     M5.Lcd.setCursor(65, 10);
     M5.Lcd.print("DIS CAL: ");
     M5.Lcd.setCursor(3, 35);
     M5.Lcd.println("DIS(cm):");
     M5.Lcd.setTextSize(9);
     M5.Lcd.setCursor(140, 70);
     M5.Lcd.print(dis);
  }
}

Credits

Amaan Javed

Amaan Javed

19 projects • 4 followers
Student

Comments