# Vending machine

**URL:** <https://forum.arduino.cc/t/vending-machine/1131681>\
**Category:** General Electronics\
**Created:** [May 27, 2023, 1:12pm UTC](https://forum.arduino.cc/t/vending-machine/1131681 "2023-05-27T13:12:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 27, 2023, 1:12pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/1 "2023-05-27T13:12:09Z")

</div>

Hi there,

Im looking to make a vending machine. I currently went out and brought a button, a dc motor, an arduino board and a motor driver. I am aiming to be able to push a button that then turns on the motor which then turns a certain amount of time at a set speed then stopping after a certain time. And i am unsure of how to do so... is there anyone that can help me with the coding and a diagram of what i need to do????

Once i get one motor and button working im aiming to have 4 buttons and 4 motors which each motor will run when the designated button is pushed.

---

<div class="post-metadata">

**Author:** ![cedarlakeinstruments](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cedarlakeinstruments/32/1128931_2.png) [@cedarlakeinstruments](https://forum.arduino.cc/u/cedarlakeinstruments)\
**Post date:** [May 27, 2023, 1:29pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/2 "2023-05-27T13:29:09Z")

</div>

This design has _no_ flexibility, but it will do exactly what you asked

```arduino
const int MOTOR_PIN = 2;
const int BUTTON_PIN = 2;
const int MOTOR_ON_MS = 1000;
void setup()
{
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(MOTOR_PIN, OUTPUT);
  digitalWrite(MOTOR_PIN, LOW);
}

void loop()
{
  delay(10);
  if (digitalRead(BUTTON_PIN) == LOW)
  {
    digitalWrite(MOTOR_PIN, HIGH);
    delay(MOTOR_ON_MS);
    digitalWrite(MOTOR_PIN, LOW);
 }
}

```

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 27, 2023, 1:44pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/3 "2023-05-27T13:44:31Z")

</div>

Is there a diagram of how to put it together?

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [May 27, 2023, 5:17pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/4 "2023-05-27T17:17:46Z")

</div>

Generally when I do a project I do the design first then buy the parts. Doing it in reverse makes it much harder to accomplish. In this case you got lucky. You should be able to make a schematic from the code, we get lots of practice here here doing that.

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 27, 2023, 11:42pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/5 "2023-05-27T23:42:20Z")

</div>

How do I do that?

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 27, 2023, 11:45pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/6 "2023-05-27T23:45:50Z")

</div>

I’ll send you photos of what I have

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 28, 2023, 2:41am UTC](https://forum.arduino.cc/t/vending-machine/1131681/7 "2023-05-28T02:41:48Z")

</div>

![IMG_3635](https://europe1.discourse-cdn.com/arduino/original/4X/4/0/3/4039bdbc446d2d0db4059e2181c7a11b79ab65da.jpeg)

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [May 28, 2023, 5:26am UTC](https://forum.arduino.cc/t/vending-machine/1131681/8 "2023-05-28T05:26:36Z")

</div>

I do not recognize the motor driver. I have worked on hundreds, mostly all different. They range from micropower to kilowatts, I realize this is nothing near that. You can solve that problem by posting links to technical information on each of the parts, the UNO I have.

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 28, 2023, 7:12am UTC](https://forum.arduino.cc/t/vending-machine/1131681/9 "2023-05-28T07:12:32Z")

</div>

the packaging of the motor says Motor 3VDC 8G/cm Hobby

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [May 28, 2023, 10:03am UTC](https://forum.arduino.cc/t/vending-machine/1131681/10 "2023-05-28T10:03:31Z")

</div>

Hi, @gazbaz11  
Welcome to the forum.

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

> [@gazbaz11](#):
>
> the packaging of the motor says Motor 3VDC 8G/cm Hobby

Can you please post a link to where you purchased your motor and driver?

Thanks... Tom.. 😀 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [May 28, 2023, 1:14pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/11 "2023-05-28T13:14:18Z")

</div>

> [@gilshultz](#):
>
> Generally when I do a project I do the design first then buy the parts.

@gazbaz11, I too design before spending money. Even to the extent of using no parts whatsoever, not even an Arduino board.

See

> **[Wokwi - Online ESP32, STM32, Arduino Simulator](https://wokwi.com)**
>
> IoT and Embedded System Simulator: ESP32, STM32, Arduino, Raspberry Pi Pico, displays, sensors, motors and WiFi simulation.

The premier (IMO) Arduino simulator, where you could do 100 percent of the software and get a very good head start on most of the hardware all with your parked on a comfortable chair in front of your desktop machine.

The real benefit to doing is that very often such zero money just time spent will inform decisions as to what, and whether, to buy.

Only more so for noobs. I can't be sure, but it looks at this point as if software is a big mystery to you, as the functionality you seek is fairly simple coding.

It seems hardware is a mystery also. Srsly consider playing with the simulator, it has plenty of "parts" you can hook together however you want all day long with no money spent and no possibility of damaging them through mistakes in wiring or power or whatever.

a7

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 28, 2023, 9:44pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/12 "2023-05-28T21:44:11Z")

</div>

Motor driver

> **[H-Bridge Motor Driver Shield for Arduino | Jaycar Electronics](https://www.jaycar.com.au/h-bridge-motor-driver-shield-for-arduino/p/XC4264?pos=4&queryId=533a9f876017cf38df84e53e8fb27d2f&sort=relevance&searchText=Motor%20driver)**
>
> SHIELD MOTOR DRVR H-BRIDGE FOR ARDUINO

Motor is

> **[Hobby Motor - Low Torque | Jaycar Electronics](https://www.jaycar.com.au/hobby-motor-low-torque/p/YM2706)**
>
> MOTOR 3VDC 8G/CM HOBBY

---

<div class="post-metadata">

**Author:** ![anon92864395](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon92864395](https://forum.arduino.cc/u/anon92864395)\
**Post date:** [May 29, 2023, 3:19am UTC](https://forum.arduino.cc/t/vending-machine/1131681/13 "2023-05-29T03:19:47Z")

</div>

Hmm..... that motor driver looks different then the one linked.

---

<div class="post-metadata">

**Author:** ![anon92864395](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon92864395](https://forum.arduino.cc/u/anon92864395)\
**Post date:** [May 29, 2023, 3:23am UTC](https://forum.arduino.cc/t/vending-machine/1131681/14 "2023-05-29T03:23:47Z")

</div>

@gilshultz  
Looks like a Keystudio L298N  
Can't find more info about it.  
If you google(or search engine) it you might find the Datasheet.

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 30, 2023, 9:41am UTC](https://forum.arduino.cc/t/vending-machine/1131681/15 "2023-05-30T09:41:08Z")

</div>

```arduino
#define button1 2
#define servo A0
int buttonPressed;

void setup() {

  pinMode(button1, INPUT);
  pinMode(servo, OUTPUT);

}
void loop() {

  // Wait until a button is pressed
  while (true){
    if (digitalRead(button1) == HIGH) 
      buttonPressed = 1;
      delay(2000);
      break;}
    
  
switch(buttonPressed) {
case 1:
  // Rotate the helical coil, discharge the selected item
  digitalWrite(servo, 700); // rotate
  delay(1290);
  digitalWrite(servo, 1500); // stop
  delay(300);
  break;}
}
thats my code so far ill send in my circuit but, im aiming for me to be able to push the button and run the motor for a certain amount of seconds

```

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 30, 2023, 9:52am UTC](https://forum.arduino.cc/t/vending-machine/1131681/16 "2023-05-30T09:52:39Z")

</div>

![Snapchat-162698057](https://europe1.discourse-cdn.com/arduino/original/4X/b/e/4/be4ffc85e254649b70c5669ae7fd21b7f67ebbfe.jpeg)

Thats the circuit and it doesnt seem to do what i need it to do. I push the button and the motor doesnt turn on it just turns off the arduino.... is there anyone that can assist me so i can get it to do what i want it to do.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [May 30, 2023, 11:17am UTC](https://forum.arduino.cc/t/vending-machine/1131681/17 "2023-05-30T11:17:07Z")

</div>

> [@gazbaz11](#):
>
> is there anyone that can assist me

Post a schematic. Hand drawn works and is fastest and easiest.

I don't have the least doubt that it will show you are trying to provide power to the motor from a source that is inadequate.

You shoukd run power directly to the motor from batteries or a good power supply, no matter how you _control_ that power.

a7

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 30, 2023, 11:56am UTC](https://forum.arduino.cc/t/vending-machine/1131681/18 "2023-05-30T11:56:53Z")

</div>

i dont understand????

---

<div class="post-metadata">

**Author:** ![gazbaz11](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@gazbaz11](https://forum.arduino.cc/u/gazbaz11)\
**Post date:** [May 30, 2023, 1:05pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/19 "2023-05-30T13:05:55Z")

</div>

![Snapchat-1283078274](https://europe1.discourse-cdn.com/arduino/original/4X/c/4/0/c40c0226d1a50b32e3a7541c079aad7df2c084c6.jpeg)  
Ive simplified it down but it still doesnt work

---

<div class="post-metadata">

**Author:** ![hammy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/hammy/32/238813_2.png) [@hammy](https://forum.arduino.cc/u/hammy)\
**Post date:** [May 30, 2023, 2:42pm UTC](https://forum.arduino.cc/t/vending-machine/1131681/20 "2023-05-30T14:42:54Z")

</div>

Spot on @alto777 You cannot use an Arduino to power a motor !! Using breadboard for it is pushing your luck too. Google will find a lot of driving motors solutions

I too design first and would re enforce that .  
For a vending machine you need to work out how it will work , look at existing machines , work out the mechanics- how does it measure , how does it drop the cup , how does it heat/ cool liquids , how does it remain hygienic ??. You might find for example that a different motor type is better , or some parts can be controlled by solenoids , or it needs a Wi-Fi connection, who knows ?

Are you going to build it , how ? Or is it a desktop exercise , in which you can use leds to stand in for motors ??

[Next page](https://forum.arduino.cc/t/vending-machine/1131681.md?page=2)
