NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Basic Electronics » Running motor through the mircocontroller

June 01, 2012
by chaosatom
chaosatom's Avatar

Hi,

I bought a small fan from the dollar tree, which uses 3V of battery. Now I want to use the motor in the fan with micro-controller. I have watched the motor video on nerdkits. I am going to be using a resistor divider to get the supply to the fan below 3V and then put it in series with a transistor controlled by PWM. The PWM will we connected to temperature sensor when I program it.

Here are the questions: 1) For the flyback diode, could I just use a LED?
2) What voltage should the transistor be biased at?

June 02, 2012
by mongo
mongo's Avatar

Rather than trying to use the 5V supply for the motor, I would suggest a power source separate from the processor supply. This is for a couple of reasons: First, motors are noisy. They can produce a hashy feedback that can scramble the operation of the processor. Secondly, the inrush current to start the motor would also wreak havoc in the supply.

An LED could be used as a flyback or clamping resistor but remember, they have a forward voltage of between 1.2 to 3 volts, where a diode is only .7 volt. (Germanium diodes brings that to .2 volt). LEDs also don't tolerate much reverse biased voltages and the induced voltage from the motor could just ruin an otherwise perfectly fine LED.

Keep the negative rail as common between the two power supplies. This gives you a reference for the signal from the processor and the driver transistor. A simple 1K to 10K resistor between the output of the processor and the base of the transistor will do nicely. I'll draw up a sketch shortly, which can do the job, and post the link when I do.

June 02, 2012
by chaosatom
chaosatom's Avatar

Thanks for the advice.

Right now I am having trouble with programming the PWM. I got some code from the tutorial. When I use my multimeter to test pin PB2/PB3, I only get like .3 V total. The average should be higher since I am running 100% duty. OCR1B/ORC1A should be the duty cycle correct? Any help would be appreciated.

Here is some snippet of code

#define PWM_START 3686.4
void pwm_set(uint16_t x) {
  OCR1B = x;
}
void pwm_init() {
  // setup Timer1 for Fast PWM mode, 16-bit
  // COM1B1 -- for non-inverting output
  // WGM13, WGM12, WGM11, WGM10 -- for Fast PWM with OCR1A as TOP value
  // CS11 -- for CLK/8 prescaling

  OCR1A = 3686.4    
  pwm_set(PWM_START);
  TCCR1A = (1<<COM1B1) | (1<<WGM11) | (1<<WGM10);
  TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS11);

  // each count is 8/14745600 = 0.5425us.
  //   20.0ms = 36864
}

In main I have:

  // set PB2,PB3 as output
  DDRB |= (1<<PB2) | (1<<PB3);

  // init PWM
  pwm_init();

  uint16_t pos = PWM_START;

  while(1) {
    pwm_set(pos);
  }
June 03, 2012
by pcbolt
pcbolt's Avatar

chaosatom -

Code looks OK. OCR1A should be the duty cycle and since you want full "ON" OCR1B should be the same (just as you have it now). The one thing I don't think is correct is setting the 2ms value to a floating point number (weird things happen when converting to integers). Try rounding 3686.4 up to a whole number (in both places). You could possibly try commenting out line 31 inside the while loop since you don't need to keep setting it over and over. (Also line 11 needs a ; at the end).

June 03, 2012
by chaosatom
chaosatom's Avatar

Thanks for the response.

I figured it out. There was nothing wrong with the code. I was accidentally "make-ing" the wrong file.

June 05, 2012
by mcgroover
mcgroover's Avatar

Hi all,

So I am trying to figure out why I can not alter the force of my solenoid using PWM. I used the above code and all values of PWM_START (0,1000,etc) output maximum force as if it was connected to the battery terminals. Even if I take away the wire to the gate from the microcontroller, I still have the maximum force present. I am thinking that I must have wired it incorrectly. 9V to drain, Ground to source (thanks pcbolt) and for the solenoid - one wire on each of the source and drain. I have a 750 Ohm resistor between the gate pin and ground.

It is my understanding that no current should flow if nothing is connected to the gate leg (apart from the resistor), is that correct? If so, any idea what I am doing wrong? There aren't that many connections, so I can't see what I am missing!

Thanks!

June 05, 2012
by chaosatom
chaosatom's Avatar

Hi,

The main thing I did was check the voltage at the PWM with a multimeter. If you increase the duty cycle, the voltage at the PWM should increase. Remember that you are measuring just the average voltage there. If it is not increasing, the code could be wrong.

Check this nerdkit on Motors if you haven't done so. http://www.nerdkits.com/videos/motors_and_microcontrollers_101/

Another thing for debugging would be to ground the gate, this will insure that transistor is off and see if the force is still present. No current will go through transistor.

June 09, 2012
by mongo
mongo's Avatar

Looks like you got the concept. PWM I assume is to control the speed? So far, the only thing I used PWM for so far is servo control.

Post a Reply

Please log in to post a reply.

Did you know that you need to think about wires differently when you're transmitting signals more than a few inches? Learn more...