Show Posts
Pages: 1 2 [3]
31  Forum 2005-2010 (read only) / Interfacing / Re: using microphone to control 3v DC motor speed on: May 01, 2007, 02:46:13 pm
ok, this is the schematic for the motor driver circuit I'm using:

The only thing that is not in my circuit is the switch. Also, I am not using 12v, as the motor is only 3v I am doing it all using 5v so the "To Motor Power Supply" goes to +5v

Here is a Schematic for the amplifier sub-circuit I am using:

The "To Arduino Analog 0" goes to Analog 0 and is used to set the delay time in the code.

Here is a photo of the circuit:

( just in case you want to see larger images: )

32  Forum 2005-2010 (read only) / Interfacing / using microphone to control 3v DC motor speed on: May 01, 2007, 01:13:43 pm
I am trying to do two things:

1)  get a microphone input to control the speed of a 3v dc motor.
2) read the values coming from a microphone via an LM386 amplifier via serial communication

It is almost working, as I understand it, as  the signal from the mic-amplifier set up gets louder the motor should slow down (as the delay value gets larger). I have had this working with a 10k pot but results were too erratic so I inserted a 1k resistor in it place (explained below).
I am after any ideas people have on how to make this work better, check the values coming from the amplifier, check that the mic is actually working properly!

The circuit I have built to do this is in two parts:
The first part controls the motor and is exactly the same as this: This all works fine.

The second part is an amplifier consisting of this <a href="">mic insert</a>
and the circuit to be found on p. 357 of Physical Computing. This is built around the LM386 amplifier. Where there is a 10k pot is in the circuit, I have replaced it with a 1k resistor for now as I don't want a pot in the final gizmo. This has also helped to make the changes in motor speed less erratic but has not solved my problems.
(If anyone has the Physical Computing book to hand....) I have basically wired the output of the amplifier to Analog in 0 on my Arduino.
I am then converting the values received on Analog0 to get the amplitude of the incoming sound.

The code given in PhysComp... is this:

' read an ADC value into the variable ADCVar, then do the following:

if adcVar >= 512 then
AmplitudeVar= adcVar - 512
AmplitudeVar = 512 - adcVar

I am not sure how this actually works but I have just re-written it as :

//read delay value from analog input (microphone)
  value = analogRead(potPin);
  //work out the amplitude (phys-comp book p.358)
  if (value >= 512) {amp = value - 512; }
  else {amp = 512 - value; }

I am then using amp to set the delay of my app and control the speed of the motor

As I said, it is kind of working, but I'd like to read what the amplifier is outputting, I've been reading from the serial port in processing (via the helpfile) and all I am getting are a string of very similar values: 48 49 52 53 57 all around that area. This is the same if I actually unplug the microphone's + connection from the breeadboard, which leads me to think something is not right......

any clues, anyone done this before??
If it's helpful I could stick a photo of the circuit up, or even a schematic of what I have if necessary.....

here is the full arduino code, it is a slight modification of that found on the site linked above:
// need to convert amp into appropriate range.

int switchPin = 2;    // switch input
int motor1Pin = 3;    // H-bridge leg 1
int motor2Pin = 4;    // H-bridge leg 2
int speedPin = 9;     // H-bridge enable pin
int ledPin = 13;      //LED
int value = 0;        // store pot value
int amp = 0;
int potPin = 0;       // Analog In 0

void setup() {
  // set the switch as an input:
  pinMode(switchPin, INPUT);
  // set all the other pins you're using as outputs:
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(speedPin, OUTPUT);
  pinMode(ledPin, OUTPUT);

  // blink the LED 3 times. This should happen only once.
  // if you see the LED blink three times, it means that the module
  // reset itself,. probably because the motor caused a brownout
  // or a short.
  blink(ledPin, 3, 100);

void loop() {
  // turn motor on:
  digitalWrite(speedPin, HIGH);
  //read delay value from analog input (microphone)
  value = analogRead(potPin);
  //work out the amplitude (phys-comp book)
  if (value >= 512) {amp = value - 512; }
  else {amp = 512 - value; }
  // if the switch is high, motor will turn on one direction:
  if (digitalRead(switchPin) == HIGH) {
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  // if the switch is low, motor will turn in the other direction:
  else {
    digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
    digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
  //turn motor off
  digitalWrite(speedPin, LOW);

  blinks an LED
void blink(int whatPin, int howManyTimes, int milliSecs) {
  int i = 0;
  for ( i = 0; i < howManyTimes; i++) {
    digitalWrite(whatPin, HIGH);
    digitalWrite(whatPin, LOW);

Pages: 1 2 [3]