Wiring dispenser/hopper with 12v motor.

hi to everyone who reads this!

I am from the UK and need some help with a project. I have a coin hopper/dispenser and want to connect it via an UNO to a microphone. the idea is that when a sound is heard (volume programmable on microphone) it will then turn the hopper motor on and allow it to dispense a set amount of coins. I had some code written for me but want to check if it would work before i go ahead and buy all of the components...!

Any help would be appreciated!

Here is code:

//********* Hopper initiated by Mic **************

#define micThreshold 500 //If the value read from the mic is above this value then a sound has been heared.
byte amountToDispenseIfSoundHeared = 10; //change this value to how many coins you want to dispense when a sound is heared

#define hopperSensorPin 3
#define motorDrivePin 2
#define micInputPin A0 //connect mic pin to analog input AO

byte amountToDispense = 0;

volatile byte amountDispensed = 0;
volatile boolean dispensing = false; //flag to track if currently dispensing

void setup() {
attachInterrupt(digitalPinToInterrupt(hopperSensorPin), coinPulse, FALLING);
pinMode(hopperSensorPin, INPUT_PULLUP);
pinMode(motorDrivePin, OUTPUT);

digitalWrite(motorDrivePin, HIGH);
Serial.println("Hopper Program Started");

void loop()
int micReading = analogRead(micInputPin);

//Serial.println(micReading); //uncomment this to see what reading is coming from the mic when a sound is heared.

if ((micReading > 500) && !dispensing) //if a reading is over the threshold and not currently dispensing then start to dispense.
amountToDispense = amountToDispenseIfSoundHeared;
digitalWrite(motorDrivePin, LOW);
dispensing = true;
delay(5); //small delay to allow the analog to digital read to settle

void coinPulse()
if (amountDispensed == amountToDispense)
digitalWrite(motorDrivePin, HIGH);
amountDispensed = 0;
dispensing = false;

Thanks :slight_smile:

I suspect the biggest problem will be getting reliable input from the microphone - and that can only be solved by testing.

I presume you will have a suitable motor driver between the Arduino and the motor. If the motor only needs to go in one direction then it could be switched on and off with a transistor or a relay. If you use a relay you won't be able to control the speed.

When posting code please use the code button </>

so your code 
looks like this

and is easy to copy to a text editor See How to use the forum