AWCSR — ESP32 simulation

ESP32 project with HC-SR04, LCD2004 I2C, and motor drivers

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by user_00.

Components

Sketch code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>

// ============================================================
// PINS
// ============================================================

#define TRIG_PIN 5
#define ECHO_PIN 18

#define SDA_PIN 21
#define SCL_PIN 22

// Arm servo
#define ARM_SERVO_PIN 25

// Sorting servo
#define SORTING_SERVO_PIN 23

// CAM UART
#define CAM_RX 16
#define CAM_TX 17

// ============================================================
// DISTANCE SETTINGS
// ============================================================

// Waste detected below/equal to 50 cm
#define STOP_DISTANCE 50.0

// Robot is allowed to resume only after object is farther
// than this distance. This prevents repeated triggering.
#define RESUME_DISTANCE 55.0

// ============================================================
// MOTOR DRIVER
// Grove I2C Motor Driver with L298
// ============================================================

#define MOTOR_DRIVER_ADDR 0x0F

#define MOTOR_SPEED_SET     0x82
#define MOTOR_DIRECTION_SET 0xAA

// M1 clockwise, M2 anticlockwise
#define FORWARD_DIRECTION 0x06

#define MOTOR_SPEED 80

// ============================================================
// LCD
// ============================================================

LiquidCrystal_I2C lcd(
  0x27,
  16,
  2
);

// ============================================================
// SERVOS
// ============================================================

Servo armServo;
Servo sortingServo;

// Small/minimal arm movement
#define ARM_HOME 45
#define ARM_PICK 70

// Small sorting movements
#define SORT_HOME     90
#define SORT_WET      65
#define SORT_DRY      80
#define SORT_PLASTIC 100
#define SORT_METAL    115

// ============================================================
// CAMERA UART
// ============================================================

HardwareSerial CamSerial(2);

// ============================================================
// ROBOT STATE
// ============================================================

enum RobotState
{
  SEARCHING,
  PROCESSING,
  WAITING_CLEAR
};

RobotState robotState = SEARCHING;

// ============================================================
// MOTOR STATE
// Prevents repeatedly sending I2C commands
// ============================================================

bool motorsRunning = false;

// ============================================================
// MOTOR SPEED
// ============================================================

void setMotorSpeed(
  byte motor1,
  byte motor2
)
{
  Wire.beginTransmission(
    MOTOR_DRIVER_ADDR
  );

  Wire.write(
    MOTOR_SPEED_SET
  );

  Wire.write(motor1);
  Wire.write(motor2);

  Wire.endTransmission();
}

// ============================================================
// MOTOR DIRECTION
// ============================================================

void setMotorDirection(
  byte direction
)
{
  Wire.beginTransmission(
    MOTOR_DRIVER_ADDR
  );

  Wire.writ

More projects by user_00