How to have knock sensor trigger motor?

Hello! I am working on a project where I want a piezo disc to trigger a motor. I am using a car door lock self actuator motor with a 9v supply ( My problem is I do not have much experience with coding. How can I use the contact mic input (knock sensor example) to trigger the output of the motor? (Tell the motor to move when "knock" happens then turn off when knock isn't happening).

Thank you! Let me know if there needs to be any more clarification!

Not sure that makes a huge amount of sense, since the length of time a knock "happens" is surely mere parts of a second? But yes if each time through the loop it checks the sensor and moves the motor a tiny bit you could do that. Next time thru the loop the knock would be "gone" and not move the motor.... but I'm not really sure what you mean.

The knock example uses, as you know, successive knocks to switch the led off-on-off etc: you could easily use that approach and just take the output from a pin to the base of a transistor through a resistor, and have the pin going off and on, switching the transistor off and on, in turn switching the motor.

Edit: About half way down this page, at the heading "protection diode" you can see how to control a motor with a transistor. The pic shows a relay, just imagine your motor in there.... the input to the transistor is from an Arduino digital pin.

Hm ok thank you for the response! I think I see what you mean... I want to have the contact mic attached to my foot connected to a long wire which goes into the arduino. So when I tap my foot or take a step then the motor will be triggered to move a drum pedal on a kick drum. I was going to use an Arduino motor shield with the motor. I was going to use this set up for connecting the motor to the pedal:

Is a knock sketch not a good idea then? What is a better way to get the motor to move enough to hit the drum?

isaralynn: Is a knock sketch not a good idea then? What is a better way to get the motor to move enough to hit the drum?

Ok I see what you mean, it probably could work.

If the sensor is "on", ie it's over the threshold as it starts loop(), then power up the motor for as long as it takes to hit the drum- perhaps experiment a bit with how long that takes. But then you would need to reverse the motor I guess, to get back to "ready". For that you'll need an h-bridge, and the easiest way to get one is to use something like this.

Instead of running the motor for a certain time to hit the drum, you could also jig it so it runs until it hits a limit switch to tell it it's got there, then stop and reverse.

EDIT... ps, maybe think of using a servo instead? Bit of trial and errror to get the degrees right, but probably easier to control. Might not be fast enough, though...

Ah but hang on.... I just read the lock spec, and it's not a motor, it's a solenoid.

If that's the case, it seems to "throw" (their word) when energised.... doesn't seem to need running like a motor. That link I posted earlier, the relay one?- should work much like that. It's not clear to me, though, if it needs to remain powered up to stay locked or how does that work?

Here is the code in the instructable used with this solenoid and arduino and motor shield: It is designed to work with maxmsp though. I assume the solenoid remains locked in the same position for the time it is powered.

Could you please tell me the arduino code I would upload to arduino?

I think you should explore using your existing “knock” code. It won’t need the state approach, which as you know makes successive knocks toggle the pin- you want each knock to activate the solenoid I think?

So change it to something like this untested code, where I’ve commented out (//) some lines and made a few changes, all marked with <<<<<<<<<… Ts & Cs apply, E&OE, YMMV.

This should power the LED for a second, each time you knock. BUT before all the other members shout at me, yes this use of delay() is bad since it locks up the Arduino for that time. You would, I’m sure, need to energise the solenoid for a tested time to make sure it’s on long enough to hit the drum before it releases. Look at the Blink Without Delay example.

int ledPin = 13;

int knockSensor = 0;               

byte val = 0;

//int statePin = LOW;   // take out <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

int THRESHOLD = 100;

void setup() {

 pinMode(ledPin, OUTPUT); 



void loop() {

  val = analogRead(knockSensor);     

  if (val >= THRESHOLD) {

    //statePin = !statePin;          // take out <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    digitalWrite(ledPin, HIGH);  // changed so that it ons the LED  <<<<<<<<<<<<<<<<<<<<<

    delay(1000);                            // added as on-time, but see note about this being bad practice <<<<<<<<<<

   digitalWrite(ledPin, LOW);  // added so that it offs the LED  <<<<<<<<<<<<<<<<<<<<<



  delay(100);  // we have to make a delay to avoid overloading the serial port


Give that a try… it’s off the top of my head and it’s 6am on Saturday so maybe not at my best yet :slight_smile: