Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

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

Image 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

  • KY-008 Laser Emitter: A module that emits a laser beam when powered.
  • IR Receiver: A sensor that detects infrared signals.
  • Arduino Mega 2560: A microcontroller board based on the ATmega2560, with numerous digital and analog I/O pins.
  • Metal Detection Sensor: A sensor that detects the presence of metal.
  • Adafruit Proto Shield R3: A prototyping shield designed to sit on top of the Arduino Mega 2560.
  • KeyeStudio Uno Prototype PCB: A prototyping PCB for Arduino Uno form factor.
  • Terminal PCB 2 Pin: A simple two-pin terminal block for making electrical connections.
  • POWER SUPPLY 12V 5AMP: A power supply unit that provides 12V and 5A output.
  • 12v to 5v Step Down Power Converter: A converter that steps down voltage from 12V to 5V.
  • LM2596 Step Down Module: A voltage regulator module that steps down input voltage to a lower output voltage.
  • HC-SR04 Ultrasonic Distance Sensor: A sensor that measures distance using ultrasonic waves.
  • Load Cell: A transducer that converts force into an electrical signal.
  • Servomotor MG90S: A small servo motor for precise control of angular position.
  • MG996R: A high-torque servo motor for precise control of angular position.
  • DC Power Source: A power source that provides DC voltage.

Wiring Details

KY-008 Laser Emitter

  • SIG connected to Arduino Mega 2560 pin D2 PWM
  • 5v connected to 5V power net
  • GND connected to Ground net

IR Receiver

  • DATA connected to Arduino Mega 2560 pin D3 PWM
  • VCC connected to 5V power net
  • GND connected to Ground net

Arduino Mega 2560

  • 5V connected to 5V power net
  • GND connected to Ground net
  • VIN connected to 12V-24V Output from the POWER SUPPLY 12V 5AMP

Metal Detection Sensor

  • POWER connected to 5V power net
  • GND connected to Ground net
  • OUT connected to Arduino Mega 2560 pin D4 PWM

HC-SR04 Ultrasonic Distance Sensor

  • VCC connected to 5V power net
  • GND connected to Ground net
  • TRIG and ECHO pins connected to Arduino Mega 2560 pins D5 PWM to D10 PWM, respectively, for three sensors.

Servomotor MG90S

  • SIG connected to Arduino Mega 2560 pin D11 PWM
  • VCC connected to 5V power net
  • GND connected to Ground net

MG996R Servomotors

  • SIG connected to Arduino Mega 2560 pins D12 PWM, D13 PWM, and D15/RX3 for three servos.
  • VCC connected to 5V power net
  • GND connected to Ground net

POWER SUPPLY 12V 5AMP

  • 220V Positive Pole (AC) connected to DC Power Source Positive
  • 220V Negative Pole (AC) connected to DC Power Source Ground
  • GND (DC) connected to Ground net
  • 12V-24V Output (DC) connected to 12v to 5v Step Down Power Converter VIN+ and Arduino Mega 2560 VIN

12v to 5v Step Down Power Converter

  • VIN 9v-36v connected to POWER SUPPLY 12V 5AMP 12V-24V Output (DC)
  • 5v OUTPUT connected to 5V power net
  • GND connected to Ground net

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.