Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560-Based Reverse Vending Machine with Servomotors and Sensors

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560-Based Reverse Vending Machine with Servomotors and Sensors

Reverse Vending Machine Circuit Documentation

Summary

The circuit is designed for a reverse vending machine controlled by an Arduino Mega 2560. It features a variety of sensors and actuators, including servomotors, an IR receiver, ultrasonic distance sensors, a metal detection sensor, and a laser emitter. The system is capable of sorting items based on material (metal or plastic) and size (small, medium, large). The circuit is powered by a 12V 5A power supply, with a step-down converter to provide 5V where necessary.

Component List

Wiring Details

KY-008 Laser Emitter

IR Receiver

Arduino Mega 2560

Metal Detection Sensor

HC-SR04 Ultrasonic Distance Sensor

Servomotor MG90S

MG996R Servomotors

POWER SUPPLY 12V 5AMP

12v to 5v Step Down Power Converter

Documented Code

/*
 * Reverse Vending Machine
 * This code controls a reverse vending machine using an Arduino Mega 2560.
 * Components:
 * - Servomotors (MG90S, MG996R)
 * - IR Receiver
 * - Ultrasonic Distance Sensors (HC-SR04)
 * - Metal Detection Sensor
 * - Laser Emitter (KY-008)
 *
 * Functionality:
 * - The first motor opens the slot for inserting items.
 * - The metal detection sensor identifies if the item is metal or plastic.
 * - The second motor sorts the item based on the detection result.
 * - Ultrasonic sensors determine the size of the item (small, medium, large).
 */

#include <Servo.h>

// Pin definitions
const int irReceiverPin = 3;
const int laserPin = 2;
const int metalSensorPin = 4;
const int trigPin1 = 5;
const int echoPin1 = 6;
const int trigPin2 = 7;
const int echoPin2 = 8;
const int trigPin3 = 9;
const int echoPin3 = 10;
const int servoPin1 = 11;
const int servoPin2 = 12;
const int servoPin3 = 13;
const int servoPin4 = 15;

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize IR receiver
  pinMode(irReceiverPin, INPUT);

  // Initialize laser emitter
  pinMode(laserPin, OUTPUT);
  digitalWrite(laserPin, LOW);

  // Initialize metal detection sensor
  pinMode(metalSensorPin, INPUT);

  // Initialize ultrasonic sensors
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);

  // Initialize servomotors
  servo1.attach(servoPin1);
  servo2.attach(servoPin2);
  servo3.attach(servoPin3);
  servo4.attach(servoPin4);

  // Set initial positions for servos
  servo1.write(0);
  servo2.write(0);
  servo3.write(0);
  servo4.write(0);
}

void loop() {
  // Check IR receiver
  if (digitalRead(irReceiverPin) == HIGH) {
    Serial.println("IR signal detected");
  }

  // Check metal detection sensor
  if (digitalRead(metalSensorPin) == HIGH) {
    Serial.println("Metal detected");
    // Sort item using servo2 (tilt right)
    servo2.write(90); // Move to right position
    delay(1000);
    servo2.write(0); // Reset position
  } else {
    Serial.println("Plastic detected");
    // Sort item using servo2 (tilt left)
    servo2.write(-90); // Move to left position
    delay(1000);
    servo2.write(0); // Reset position
  }

  // Measure distance with ultrasonic sensors
  long distance1 = measureDistance(trigPin1, echoPin1);
  long distance2 = measureDistance(trigPin2, echoPin2);
  long distance3 = measureDistance(trigPin3, echoPin3);

  Serial.print("Distance 1: ");
  Serial.print(distance1);
  Serial.print(" cm, Distance 2: ");
  Serial.print(distance2);
  Serial.print(" cm, Distance 3: ");
  Serial.print(distance3);
  Serial.println(" cm");

  // Determine size based on distance
  if (distance1 < 10) {
    Serial.println("Small item detected");
    // Handle small item
  } else if (distance2 < 20) {
    Serial.println("Medium item detected");
    // Handle medium item
  } else if (distance3 < 30) {
    Serial.println("Large item detected");
    // Handle large item
  }

  delay(1000);
}

long measureDistance(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  long distance = duration * 0.034 / 2;
  return distance;
}

This code is responsible for the operation of the reverse vending machine. It initializes the sensors and actuators, reads sensor data, and controls the servomotors to sort items based on material and size. The measureDistance function is used to calculate the distance measured by the ultrasonic sensors.