我们不仅销售产品,我们还传播知识
商品分类

esp32 称重传送带开源项目

2024102207085575

#include <LiquidCrystal.h>

#include “HX711.h”

// Ultrasonic Sensor Pins

#define TRIG_PIN 18

#define ECHO_PIN 19

#define MAX_DISTANCE 200 // Maximum distance to detect objects (in cm)

// Stepper Motor Pins

#define MOTOR_PIN_A1 21

#define MOTOR_PIN_A2 22

#define MOTOR_PIN_B1 23

#define MOTOR_PIN_B2 25

// Load Cell Pins

#define LOADCELL_DOUT_PIN 4

#define LOADCELL_SCK_PIN 5

// LED Pins

#define RED_LED_PIN 15

#define GREEN_LED_PIN 14

// Load Cell Threshold

#define WEIGHT_THRESHOLD 25.0 // 25 kg

HX711 scale;

// Stepper Motor Steps

int stepNumber = 0;

// LCD Display Pins

LiquidCrystal lcd(22, 21, 19, 18, 5, 17); // Define pins for LCD (RS, E, D4, D5, D6, D7)

void setup() {

  // Initialize Serial Monitor

  Serial.begin(115200);

  // Initialize LCD

  lcd.begin(16, 2); // Specify the number of columns and rows

  lcd.setCursor(0, 0);

  lcd.print(“Conveyor Status:”);

  // Set up Ultrasonic Sensor Pins

  pinMode(TRIG_PIN, OUTPUT);

  pinMode(ECHO_PIN, INPUT);

  // Set up Stepper Motor Pins

  pinMode(MOTOR_PIN_A1, OUTPUT);

  pinMode(MOTOR_PIN_A2, OUTPUT);

  pinMode(MOTOR_PIN_B1, OUTPUT);

  pinMode(MOTOR_PIN_B2, OUTPUT);

  // Initialize Load Cell

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);

  scale.set_scale(); // You should calibrate your scale using a known weight and then set the scale

  // Set up LED Pins

  pinMode(RED_LED_PIN, OUTPUT);

  pinMode(GREEN_LED_PIN, OUTPUT);

  // Initially stop the motor

  stopMotor();

  lcd.setCursor(0, 1);

  lcd.print(“Motor Stops   “); // Display “Motor Stops”

}

void loop() {

  // Measure distance using the ultrasonic sensor

  long distance = measureDistance();

  // Measure weight using the load cell

  float weight = scale.get_units(10); // Get the average of 10 readings

  // Control the conveyor belt based on distance

  if (distance > 0 && distance < MAX_DISTANCE) {

    Serial.println(“Object detected! Starting conveyor belt.”);

    stepMotor(); // Step the motor

    lcd.setCursor(0, 1);

    lcd.print(“Motor Starts   “); // Display “Motor Starts”

  } else {

    Serial.println(“No object detected. Stopping conveyor belt.”);

    stopMotor(); // Stop the motor if no object is detected

    lcd.setCursor(0, 1);

    lcd.print(“Motor Stops   “); // Display “Motor Stops”

  }

  // Control LEDs based on weight

  if (weight > WEIGHT_THRESHOLD) {

    digitalWrite(RED_LED_PIN, HIGH);

    digitalWrite(GREEN_LED_PIN, LOW);

    Serial.println(“Weight above threshold, Red LED ON, Green LED OFF”);

  } else {

    digitalWrite(RED_LED_PIN, LOW);

    digitalWrite(GREEN_LED_PIN, HIGH);

    Serial.println(“Weight below threshold, Red LED OFF, Green LED ON”);

  }

  // Print weight to Serial Monitor

  Serial.print(“Weight: “);

  Serial.print(weight);

  Serial.println(” kg”);

  delay(100); // Delay for stability

}

void stepMotor() {

  switch (stepNumber) {

    case 0:

      digitalWrite(MOTOR_PIN_A1, HIGH);

      digitalWrite(MOTOR_PIN_A2, LOW);

      digitalWrite(MOTOR_PIN_B1, HIGH);

      digitalWrite(MOTOR_PIN_B2, LOW);

      break;

    case 1:

      digitalWrite(MOTOR_PIN_A1, LOW);

      digitalWrite(MOTOR_PIN_A2, HIGH);

      digitalWrite(MOTOR_PIN_B1, HIGH);

      digitalWrite(MOTOR_PIN_B2, LOW);

      break;

    case 2:

      digitalWrite(MOTOR_PIN_A1, LOW);

      digitalWrite(MOTOR_PIN_A2, HIGH);

      digitalWrite(MOTOR_PIN_B1, LOW);

      digitalWrite(MOTOR_PIN_B2, HIGH);

      break;

    case 3:

      digitalWrite(MOTOR_PIN_A1, HIGH);

      digitalWrite(MOTOR_PIN_A2, LOW);

      digitalWrite(MOTOR_PIN_B1, LOW);

      digitalWrite(MOTOR_PIN_B2, HIGH);

      break;

  }

  stepNumber++;

  if (stepNumber > 3) {

    stepNumber = 0;

  }

  delay(10); // Adjust delay for motor speed

}

void stopMotor() {

  digitalWrite(MOTOR_PIN_A1, LOW);

  digitalWrite(MOTOR_PIN_A2, LOW);

  digitalWrite(MOTOR_PIN_B1, LOW);

  digitalWrite(MOTOR_PIN_B2, LOW);

}

long measureDistance() {

  // Send a 10us pulse to trigger the ultrasonic sensor

  digitalWrite(TRIG_PIN, LOW);

  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);

  delayMicroseconds(10);

  digitalWrite(TRIG_PIN, LOW);

  // Read the echo pin, returns the sound wave travel time in microseconds

  long duration = pulseIn(ECHO_PIN, HIGH);

  // Calculate the distance (speed of sound is 34300 cm/s)

  long distance = duration * 0.034 / 2;

  return distance;

}

项目地址:https://wokwi.com/projects/405170590102260737

https://www.chuang-ke.com/19528

发表回复

登录后才能评论
微信群
微信群
联系我们

联系我们

微信:13823392571

在线咨询:点击这里给我发消息

工作时间:周一至周五,9:30-18:30,节假日休息

微信客服
微信客服
分享本页
返回顶部