Hi I'm new here and a student , I'm making a vending machine project

My project is about vending machine and I don't have a code for it , this project run on Arduino and the components I will use are, (Arduino mega 2560, 2 28BYJ-48 Stepper motor with ULN2003 driver, 2 ultrasonic sensor, 1 coin slot acceptor, 1 active buzzer 5v, 1 rgb module, 2 medium size breadboard, 1 tacktile button switch, 1I2c LCD display, and for the power supply is LRS-50-12).

It will be helpful if you help me with the coding.

And the project title is " test booklet vending machine".
Thank you!

Glad to and welcome to the Arduino forum. Begin by writing a program to exercise EACH individual component all by itself so you understand how it responds and works.

Only then will you have enough skill to begin to put together a final program for your vending machine.

You failed to list the power supplies you will be needing for your project, so get them together first so you can make each component operate.

1 Like

You need to draw a wiring diagram with all the devices you mentioned.

1 Like

That sounds like an interesting project that could be a lot of fun! However, please keep in mind that we are not a free design or code-writing service. By writing your code we are helping you cheat and you will not learn what you should. That will only hurt your in the future. We’re more than happy to help with your design or code, but we need you to make an initial attempt. Please design and write your code, then post it along with an explanation of what’s not working properly. There is also a for hire section if you want to pay for it.

Follow @Paul_KD7HB advice and break it down into simple sections. Once you do that you have about 25% of the project finished.

  1. Show Your Work First: Before asking for assistance, make an attempt to design or write the code yourself. Share your work along with details about what isn’t working.
  2. Provide Clear Documentation: Since we can’t see your project, share an annotated schematic (best) or a clear drawing of your setup. Pictures are welcome, but avoid using Fritzing diagrams as they are wiring diagrams, not schematics, and are not ideal for troubleshooting.
  3. Include Technical Details: If there is specific hardware involved, include links to technical information. There are often many versions of similar components, so precise details are essential.
  4. Reference Resources: For additional help, check out useful links and tutorials: Useful Links on Arduino Forum.
1 Like

How helpful did you find the five related topics at the bottom of the thread?

1 Like

this is the wirring for my project

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

// === LCD ===
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust address if needed

// === Coin & Button ===
const int coinPin = 2;       // Interrupt pin for coin
const int buttonPin = 3;     // Pushbutton

// === LED Pins ===
const int redLED = 4;
const int greenLED = 5;
const int blueLED = 6;

// === Buzzer ===
const int buzzerPin = 7;

// === Ultrasonic Sensors ===
const int trig1 = 8, echo1 = 9;   // Paper stock sensor
const int trig2 = 10, echo2 = 11; // Delivery confirmation sensor

// === Stepper Motors ===
AccelStepper stepper1(AccelStepper::FULL4WIRE, 22, 24, 26, 28);
AccelStepper stepper2(AccelStepper::FULL4WIRE, 30, 32, 34, 36);

// === Variables ===
volatile float balance = 0.0;
bool releasing = false;

void setup() {
  // LCD Setup
  lcd.init();
  lcd.backlight();

  // I/O Setup
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(coinPin, INPUT_PULLUP);

  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  pinMode(trig1, OUTPUT);
  pinMode(echo1, INPUT);
  pinMode(trig2, OUTPUT);
  pinMode(echo2, INPUT);

  // Stepper Config
  stepper1.setMaxSpeed(500);
  stepper2.setMaxSpeed(500);

  // Interrupt
  attachInterrupt(digitalPinToInterrupt(coinPin), coinInserted, FALLING);

  // Welcome Message
  lcd.setCursor(0, 0);
  lcd.print("Test Booklet");
  lcd.setCursor(0, 1);
  lcd.print("Vending Machine");
  delay(1000);
  lcd.clear();
  delay(1000);
}

void loop() {
  if (!checkStock()) {
    alertEmptyStock();
    return;
  }

  showBalance();

  if (digitalRead(buttonPin) == LOW && balance >= 10.0 && !releasing) {
    releaseBooklet();
  }
}

void coinInserted() {
  delay(50); // Debounce
  balance = 10.0;
  digitalWrite(blueLED, HIGH);
}

void showBalance() {
  lcd.setCursor(0, 0);
  lcd.print("INSERT COIN!!   ");
  lcd.setCursor(0, 1);
  lcd.print("bal.php: ");
  lcd.print(balance, 2);
}

void releaseBooklet() {
  releasing = true;

  digitalWrite(blueLED, LOW);
  digitalWrite(greenLED, HIGH);
  digitalWrite(buzzerPin, HIGH);

  // Stepper Motor 1: 360°
  stepper1.moveTo(200);  // Adjust if not 1 rev
  while (stepper1.distanceToGo() != 0) stepper1.run();

  delay(2000);

  // Stepper Motor 2: 180°
  stepper2.moveTo(100);  // Adjust if not 180°
  while (stepper2.distanceToGo() != 0) stepper2.run();

  // End of movement
  digitalWrite(greenLED, LOW);
  digitalWrite(buzzerPin, LOW);
  digitalWrite(redLED, HIGH);
  delay(1000);
  digitalWrite(redLED, LOW);
  lcd.clear();
  delay(1000);

  // Delivery Confirmation
  if (confirmDelivery()) {
    lcd.setCursor(0, 0);
    lcd.print("Done");
    delay(1000);
    lcd.clear();
    lcd.print("Thank you bye");
    delay(1000);
    lcd.clear();
  } else {
    lcd.print("Delivery Failed");
    delay(2000);
    lcd.clear();
  }

  // Reset
  balance = 0.0;
  releasing = false;
  digitalWrite(blueLED, LOW);
}

bool checkStock() {
  float distance = readUltrasonic(trig1, echo1);
  return distance < 10;  // Threshold in cm
}

bool confirmDelivery() {
  float distance = readUltrasonic(trig2, echo2);
  return distance < 10;  // Adjust threshold
}

float readUltrasonic(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH, 20000); // Timeout 20ms
  return duration * 0.034 / 2;  // cm
}

void alertEmptyStock() {
  for (int i = 0; i < 5; i++) {
    digitalWrite(redLED, HIGH);
    digitalWrite(buzzerPin, HIGH);
    delay(300);
    digitalWrite(redLED, LOW);
    digitalWrite(buzzerPin, LOW);
    delay(300);
  }
  lcd.clear();
  lcd.print("REFILL PAPER");
  delay(2000);
  lcd.clear();
}


this is the code i use.

ok, does it all work as you want it to do?

1 Like

In your testing, what does your calculation show when you combine integer and float? What happens when the duration is zero?

1 Like

Sorry, sir, but this code was not written by me. This was generated by AI, because this is my first time doing the project, and I have a small amount of knowledge of writing code. But I'm willingly wanting to learn more about it.

You will not get anything working with "AI".
Learn programming in C.
Learn about Arduino Mega2560.
Practice the built-in examples.

Practice with the Serial Monitor:

void setup() {
  Serial.begin(115200);
  Serial.print("Hello, World!");
}

void loop() {
  // empty
}

Practice with the LCD:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS EN D4 D5 D6 D7

void setup() {
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("Arduino UNO");
  lcd.setCursor(1, 1);
  lcd.print("Ping Pong Game");
}

void loop() {}

Practice button reading

byte buttonpin = 2; // wiring: pin >> button (normally open) >> GND

void setup() {
  Serial.begin(115200); // start serial communications
  pinMode(buttonpin, INPUT_PULLUP); // tie button pin HIGH.
}

void loop() {
  if (digitalRead(buttonpin) == LOW) { // button press is LOW
    delay(150); // debounce button press
    Serial.print(buttonpin); // print buttonpin number
  }
}

Practice HC-SR04

#define PIN_ECHO 2
#define PIN_TRIG 3

void setup() {
  Serial.begin(115200);
  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECHO, INPUT);
}

void loop() {
  digitalWrite(PIN_TRIG, HIGH); // start a trigger pulse
  delayMicroseconds(10); // make the trigger pulse 10ms long
  digitalWrite(PIN_TRIG, LOW); // stop the trigger pulse

  int duration = pulseIn(PIN_ECHO, HIGH); // listen for the return pulse and record
  Serial.print("Distance in CM: ");
  Serial.println(duration / 58); // show the distance calculation
  delay(1000); // wait for one second
}

Practice with a buzzer...

#define buzzer 10

void setup() {
  pinMode(buzzer, OUTPUT);
}
void loop() {
  buzzerSound(50);
  delay(1000);
}

void buzzerSound(int mydelay) {
  analogWrite(buzzer, 20);
  delay(mydelay);
  analogWrite(buzzer, 0);
}

Practice with a servo (if you have one):

#include <Servo.h>
Servo myservo;
int servopin = 9;

void setup() {
  myservo.attach(servopin);
}

void loop() {
  for (int i = 0; i < 180; i++) { // degrees
    myservo.write(i);
    delay(15);
  }
  delay(1000);
  for (int i = 0; i < 180; i++) { // degrees
    myservo.write(180 - i);
    delay(15);
  }
  delay(1000);
}

You should also practice with one ULN2003 and its stepper motor using AccelStepper

#include <AccelStepper.h>

#define FULLSTEP 4
AccelStepper stepper(FULLSTEP, 11, 9, 10, 8); // 4-wire, non-default pinning
#define STEP_PER_REVOLUTION 2048 // see datasheet

void setup() {
  Serial.begin(115200);
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(50);
  stepper.setSpeed(200);
  stepper.setCurrentPosition(0); // start at 0
  stepper.moveTo(STEP_PER_REVOLUTION); // set target of 2048
}

void loop() {
  if (stepper.distanceToGo() == 0)
    stepper.moveTo(-stepper.currentPosition()); // change direction at target
  stepper.run(); // MUST call in loop()
  Serial.print(F("Current Position: "));
  Serial.println(stepper.currentPosition());
}
1 Like

When is this due?

The usual scheme for learning is to begin at the beginning, not at the end and work back to the beginning. Is this the new way of teaching?

Edit: I just saw a news story about a teacher quitting teaching for this very reason. So, I guess we can expect much more of these requests.

1 Like

Best way to teach swimming? Toss them all in deep water, graduate those that make it. Less effort and stress for the teacher, and a sense of real accomplishment for the grads.

Apologies, back to your regular programming...

1 Like

No sir. Sometime if I plug it in it the display show any kind of symbols and the other components doesn't work..

But here the primarily function I want to this project..

We using Arduino at mega 2560 . 2 pcs stepper motor . 1pcs active buzzer 3-24 active buzzer. 2 pcs Ultrasonic sensor Red green blue module led. 12 c lcd . Tack button switch . 12v power supply.

Follow my instructions to make a correct code

Here's a detailed explanation of the functional flow of your test booklet vending machine based on the provided code:


  1. Power-On Sequence

System powers on.

LCD displays: Test Booklet Vending Machine for 1 second.

LCD clears, waits 1 second.

LCD then shows: INSERT COIN!! bal.php: 0.00


  1. Coin Insertion

When a 10 peso coin is inserted:

An interrupt detects the coin.

Balance increases to 10.0.

LCD updates: INSERT COIN!! bal.php: 10.00

Blue LED turn ON to show coin has been accepted.


  1. Button Press (User Request)

If the user presses the push button:

System checks if the balance is at least 10.0 and not already processing.

Enters release mode:

Blue LED turns OFF

Green LED turns ON

Buzzer turns ON (indicates paper is being released)


  1. Stepper Motor Action

Stepper Motor 1 rotates 360° (simulates feeding or grabbing the paper).

Waits 2 seconds.

Stepper Motor 2 rotates 180° (opens or pushes paper out).

After both motors complete:

Green LED turns OFF

Buzzer turns OFF

Red LED turns ON

Wait 1 second, then:

Red LED turns OFF

LCD clears and waits another second.


  1. Ultrasonic Sensor Checks Ultrasonic Sensor 1:

Continuously checks if paper stock is present.

If no paper:

Red LED blinks for 5 times indicating need to be refilled

Buzzer beeps

System halts releasing until restocked.

Ultrasonic Sensor 2:

Confirms if paper was successfully delivered.

If yes:

LCD shows Done message Delay 1 second lcd clear

Lcd display "thank you bye"

Delay 1 second ,lcd clear Wait another second.

Lcd resets to : Insert coin!! Bal.php: 00.0

This project due this coming may 19.

So, you followed instructions and created a program... but the code or your hardware are not working. You should test each device on its own, then add one device, and continue until finished.

1 Like

Ai generated code

That will certainly help a lot!

Sorry sir. Can you help me write another code but it has the same function in the rply I give