DC motor control with pwmout

RodrigoFramrose

New Member
Good afternoon,

I got some project stuff from school and i started working more with picaxe and my teacher adviced me to use pwmout to control a DC motor, i searched a few things about it but couldn't understand so well, would be to ask so much if you guys can show me a simple example from a DC motor control with 'pwmout' ? I found this forum and i guess we can take better conclusions talking with each other.

I'll be waiting for help guys,

Regards,

Diogo Cardoso
 

inglewoodpete

Senior Member
Hi Diogo, Welcome to the PICAXE Forum.

Several years ago, I had a summer-time overheating problem in an electronic instrument (PVR) and developed this code to drive a DC motor/fan to help keep it cool.

You probably already understand how PWM works. The PWM source (PICAXE in this case) switches quickly between 0v and 5v (representing logic 0 and 1), controlling the amount of '1' versus '0'. This varies the average amount of energy being delivered to the load and can control the brightness of a light or the speed of a motor.

Motors have characteristics that you need to consider. Once a motor is running, it needs less energy than if it has to be started from its resting position. So, when starting the stationary motor, I give the motor pin a longer pulse at full power to get the motor's armature moving before turning on the PWM control.

You will not be able to connect a motor directly to a PICAXE but will need to use a bipolar transistor, darlington transistor or MOSFET to handle the higher current and/or voltage required by a motor. For interfacing circuitry, refer to Manual 3, "Output Devices 5 - Motors".

As I said before, this code was written several years ago for an older PICAXE model (14M) as may need some minor command syntax changes to make it work on a more modern PICAXE.

Code:
[color=Green]'PVR Cooler controlled by a PICAXE 14M2 and an instrument cooling fan.[/color]
[color=Navy]#Picaxe [/color][color=Black]14M[/color]
[color=Navy]#Terminal 4800[/color]
[color=Green]'
'Inputs have the prefix 'i'[/color]
[color=Blue]Symbol iPVRTempSense   [/color][color=DarkCyan]= [/color][color=Navy]0      [/color][color=Green]'14M Leg 7 DS18B20 Sensor for PVR  temperature[/color]
[color=Blue]Symbol iAmbientSense   [/color][color=DarkCyan]= [/color][color=Navy]4      [/color][color=Green]'14M Leg 3 DS18B20 Sensor for room temperature
'
'14M Port C: B/W used as outputs[/color]
[color=Blue]Symbol oPWM_Fan        [/color][color=DarkCyan]= [/color][color=Blue]C.2    [/color][color=Green]'14M Leg 5 Port C5 used as PWMOut output - called '2'
'
'Outputs have the prefix 'o'[/color]
[color=Blue]Symbol oLEDBooted      [/color][color=DarkCyan]= [/color][color=Navy]5      [/color][color=Green]'    Leg 8   LED to indicate PICAXE has (re)booted
'
'Variables - Byte variables start with 'b'; Word Variables start with 'w'[/color]
[color=Blue]Symbol [/color][color=Purple]bAmbientTemp  [/color][color=DarkCyan]= [/color][color=Purple]b4[/color]
[color=Blue]Symbol [/color][color=Purple]bPVRTemp      [/color][color=DarkCyan]= [/color][color=Purple]b5[/color]
[color=Blue]Symbol [/color][color=Purple]bTempDiff     [/color][color=DarkCyan]= [/color][color=Purple]b6      [/color][color=Green]'Temperature difference (Ambient vs inside electronic instrument)[/color]
[color=Blue]Symbol [/color][color=Purple]bFanSpeed     [/color][color=DarkCyan]= [/color][color=Purple]b7[/color]
[color=Blue]Symbol [/color][color=Purple]bFanOn        [/color][color=DarkCyan]= [/color][color=Purple]b8[/color]
[color=Green]'
'Constants[/color]
[color=Blue]Symbol False         [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Blue]Symbol True          [/color][color=DarkCyan]= [/color][color=Navy]1[/color]
[color=Green]'
'[/color]
[color=Black]Init: [/color][color=Blue]High oLEDBooted
      Pause [/color][color=Navy]3000                        [/color][color=Green]'Indicate PICAXE has booted or rebooted
      [/color][color=Blue]Low oLEDBooted
      PWMOut oPWM_Fan[/color][color=Black], [/color][color=Navy]49[/color][color=Black], [/color][color=Navy]0            [/color][color=Green]'Ensure PWM pin is output (0% PWM Duty)
      '
      [/color][color=Blue]Do
         [/color][color=Green]'Readtemp12 AmbientSense, w5   'ReadTemp12 not supported by FW ver 9.C
         [/color][color=Blue]Readtemp iAmbientSense[/color][color=Black], [/color][color=Purple]bAmbientTemp
         [/color][color=Green]'
         'Readtemp12 PVRTempSense, w6   'PVRTemp
         [/color][color=Blue]Readtemp iPVRTempSense[/color][color=Black], [/color][color=Purple]bPVRTemp
         [/color][color=Green]'
         [/color][color=Purple]bTempDiff [/color][color=DarkCyan]= [/color][color=Purple]bPVRTemp [/color][color=DarkCyan]- [/color][color=Purple]bAmbientTemp[/color][color=Green]'Assumes PVR temperature will be hotter or equal to ambient
         [/color][color=Blue]If [/color][color=Purple]bPVRTemp [/color][color=DarkCyan]> [/color][color=Navy]35 [/color][color=Blue]Then            [/color][color=Green]'Over 35deg C
            [/color][color=Blue]PulsOut [/color][color=Navy]1[/color][color=Black], [/color][color=Navy]1000
            [/color][color=Blue]PWMOut [/color][color=Navy]2[/color][color=Black], [/color][color=Navy]49[/color][color=Black], [/color][color=Navy]200            [/color][color=Green]'Maximum speed
         [/color][color=Blue]ElseIf [/color][color=Purple]bTempDiff [/color][color=DarkCyan]> [/color][color=Navy]0 [/color][color=Blue]Then
            If [/color][color=Purple]bFanOn [/color][color=DarkCyan]= [/color][color=Blue]False Then
               [/color][color=Purple]bFanOn [/color][color=DarkCyan]= [/color][color=Blue]True
               High oPWM_Fan             [/color][color=Green]'Burst of 12v ensures fans starts properly
               [/color][color=Blue]Pause [/color][color=Navy]300                 [/color][color=Green]'300mS burst
               [/color][color=Blue]Low oPWM_Fan
            EndIf
            If [/color][color=Purple]bTempDiff [/color][color=DarkCyan]> [/color][color=Navy]7 [/color][color=Blue]Then         [/color][color=Green]'7 degrees C up means full speed: limit it to 7
               [/color][color=Purple]bTempDiff [/color][color=DarkCyan]= [/color][color=Navy]7
            [/color][color=Blue]EndIf
            [/color][color=Purple]bFanSpeed [/color][color=DarkCyan]= [/color][color=Purple]bTempDiff [/color][color=DarkCyan]- [/color][color=Navy]1 [/color][color=DarkCyan]* [/color][color=Navy]25 [/color][color=DarkCyan]+ [/color][color=Navy]40[/color][color=Green]'Gives 7 steps: 40, 65, 90, 115, 140, 165, 190
            [/color][color=Blue]PWMOut oPWM_Fan[/color][color=Black], [/color][color=Navy]49[/color][color=Black], [/color][color=Purple]bFanSpeed [/color][color=Green][PLAIN]'FanSpeed=100=50%; FanSpeed=200=100%; Eg  pwmout 2 , 49, 100 '[50%][/PLAIN]
         [/color][color=Blue]Else
            PWMOut oPWM_Fan[/color][color=Black], [/color][color=Navy]49[/color][color=Black], [/color][color=Navy]0       [/color][color=Green]'Just put here during development
            [/color][color=Purple]bFanOn [/color][color=DarkCyan]= [/color][color=Blue]False
         EndIf
         [/color][color=Green]'
         [/color][color=Blue]Pause [/color][color=Navy]4000
      [/color][color=Blue]Loop[/color]
 

DDGarcia

New Member
Hi Diogo, Welcome to the PICAXE Forum.

Several years ago, I had a summer-time overheating problem in an electronic instrument (PVR) and developed this code to drive a DC motor/fan to help keep it cool.

You probably already understand how PWM works. The PWM source (PICAXE in this case) switches quickly between 0v and 5v (representing logic 0 and 1), controlling the amount of '1' versus '0'. This varies the average amount of energy being delivered to the load and can control the brightness of a light or the speed of a motor.

Motors have characteristics that you need to consider. Once a motor is running, it needs less energy than if it has to be started from its resting position. So, when starting the stationary motor, I give the motor pin a longer pulse at full power to get the motor's armature moving before turning on the PWM control.

You will not be able to connect a motor directly to a PICAXE but will need to use a bipolar transistor, darlington transistor or MOSFET to handle the higher current and/or voltage required by a motor. For interfacing circuitry, refer to Manual 3, "Output Devices 5 - Motors".

As I said before, this code was written several years ago for an older PICAXE model (14M) as may need some minor command syntax changes to make it work on a more modern PICAXE.

Code:
[color=Green]'PVR Cooler controlled by a PICAXE 14M2 and an instrument cooling fan.[/color]
[color=Navy]#Picaxe [/color][color=Black]14M[/color]
[color=Navy]#Terminal 4800[/color]
[color=Green]'
'Inputs have the prefix 'i'[/color]
[color=Blue]Symbol iPVRTempSense   [/color][color=DarkCyan]= [/color][color=Navy]0      [/color][color=Green]'14M Leg 7 DS18B20 Sensor for PVR  temperature[/color]
[color=Blue]Symbol iAmbientSense   [/color][color=DarkCyan]= [/color][color=Navy]4      [/color][color=Green]'14M Leg 3 DS18B20 Sensor for room temperature
'
'14M Port C: B/W used as outputs[/color]
[color=Blue]Symbol oPWM_Fan        [/color][color=DarkCyan]= [/color][color=Blue]C.2    [/color][color=Green]'14M Leg 5 Port C5 used as PWMOut output - called '2'
'
'Outputs have the prefix 'o'[/color]
[color=Blue]Symbol oLEDBooted      [/color][color=DarkCyan]= [/color][color=Navy]5      [/color][color=Green]'    Leg 8   LED to indicate PICAXE has (re)booted
'
'Variables - Byte variables start with 'b'; Word Variables start with 'w'[/color]
[color=Blue]Symbol [/color][color=Purple]bAmbientTemp  [/color][color=DarkCyan]= [/color][color=Purple]b4[/color]
[color=Blue]Symbol [/color][color=Purple]bPVRTemp      [/color][color=DarkCyan]= [/color][color=Purple]b5[/color]
[color=Blue]Symbol [/color][color=Purple]bTempDiff     [/color][color=DarkCyan]= [/color][color=Purple]b6      [/color][color=Green]'Temperature difference (Ambient vs inside electronic instrument)[/color]
[color=Blue]Symbol [/color][color=Purple]bFanSpeed     [/color][color=DarkCyan]= [/color][color=Purple]b7[/color]
[color=Blue]Symbol [/color][color=Purple]bFanOn        [/color][color=DarkCyan]= [/color][color=Purple]b8[/color]
[color=Green]'
'Constants[/color]
[color=Blue]Symbol False         [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Blue]Symbol True          [/color][color=DarkCyan]= [/color][color=Navy]1[/color]
[color=Green]'
'[/color]
[color=Black]Init: [/color][color=Blue]High oLEDBooted
      Pause [/color][color=Navy]3000                        [/color][color=Green]'Indicate PICAXE has booted or rebooted
      [/color][color=Blue]Low oLEDBooted
      PWMOut oPWM_Fan[/color][color=Black], [/color][color=Navy]49[/color][color=Black], [/color][color=Navy]0            [/color][color=Green]'Ensure PWM pin is output (0% PWM Duty)
      '
      [/color][color=Blue]Do
         [/color][color=Green]'Readtemp12 AmbientSense, w5   'ReadTemp12 not supported by FW ver 9.C
         [/color][color=Blue]Readtemp iAmbientSense[/color][color=Black], [/color][color=Purple]bAmbientTemp
         [/color][color=Green]'
         'Readtemp12 PVRTempSense, w6   'PVRTemp
         [/color][color=Blue]Readtemp iPVRTempSense[/color][color=Black], [/color][color=Purple]bPVRTemp
         [/color][color=Green]'
         [/color][color=Purple]bTempDiff [/color][color=DarkCyan]= [/color][color=Purple]bPVRTemp [/color][color=DarkCyan]- [/color][color=Purple]bAmbientTemp[/color][color=Green]'Assumes PVR temperature will be hotter or equal to ambient
         [/color][color=Blue]If [/color][color=Purple]bPVRTemp [/color][color=DarkCyan]> [/color][color=Navy]35 [/color][color=Blue]Then            [/color][color=Green]'Over 35deg C
            [/color][color=Blue]PulsOut [/color][color=Navy]1[/color][color=Black], [/color][color=Navy]1000
            [/color][color=Blue]PWMOut [/color][color=Navy]2[/color][color=Black], [/color][color=Navy]49[/color][color=Black], [/color][color=Navy]200            [/color][color=Green]'Maximum speed
         [/color][color=Blue]ElseIf [/color][color=Purple]bTempDiff [/color][color=DarkCyan]> [/color][color=Navy]0 [/color][color=Blue]Then
            If [/color][color=Purple]bFanOn [/color][color=DarkCyan]= [/color][color=Blue]False Then
               [/color][color=Purple]bFanOn [/color][color=DarkCyan]= [/color][color=Blue]True
               High oPWM_Fan             [/color][color=Green]'Burst of 12v ensures fans starts properly
               [/color][color=Blue]Pause [/color][color=Navy]300                 [/color][color=Green]'300mS burst
               [/color][color=Blue]Low oPWM_Fan
            EndIf
            If [/color][color=Purple]bTempDiff [/color][color=DarkCyan]> [/color][color=Navy]7 [/color][color=Blue]Then         [/color][color=Green]'7 degrees C up means full speed: limit it to 7
               [/color][color=Purple]bTempDiff [/color][color=DarkCyan]= [/color][color=Navy]7
            [/color][color=Blue]EndIf
            [/color][color=Purple]bFanSpeed [/color][color=DarkCyan]= [/color][color=Purple]bTempDiff [/color][color=DarkCyan]- [/color][color=Navy]1 [/color][color=DarkCyan]* [/color][color=Navy]25 [/color][color=DarkCyan]+ [/color][color=Navy]40[/color][color=Green]'Gives 7 steps: 40, 65, 90, 115, 140, 165, 190
            [/color][color=Blue]PWMOut oPWM_Fan[/color][color=Black], [/color][color=Navy]49[/color][color=Black], [/color][color=Purple]bFanSpeed [/color][color=Green][PLAIN]'FanSpeed=100=50%; FanSpeed=200=100%; Eg  pwmout 2 , 49, 100 '[50%][/PLAIN]
         [/color][color=Blue]Else
            PWMOut oPWM_Fan[/color][color=Black], [/color][color=Navy]49[/color][color=Black], [/color][color=Navy]0       [/color][color=Green]'Just put here during development
            [/color][color=Purple]bFanOn [/color][color=DarkCyan]= [/color][color=Blue]False
         EndIf
         [/color][color=Green]'
         [/color][color=Blue]Pause [/color][color=Navy]4000
      [/color][color=Blue]Loop[/color]
I'm not sure but you said Mosfat for high current devices right?
I'm just asking because I am doing a DC pump micro water system and I will problably endup using Pwmout command too.
But as far as eletronics, I'm using an ULN2003A that drives the supply of the power supply to the pump, ( Pump = 12V max 2A) , and control it with pwm out
 

erco

Senior Member
Welcome, Diogo! You'll need to build your hardware first. If you don't need to reverse the motor (i.e, just vary the speed in one direction), you can probably use a single power transistor or mosfet. To reverse the motor you will need an H-bridge, which is usually cheaper & easier to buy than make. You should share some info on your motor size, voltage & current for further help.

$1.81 L298 H-bridge: https://www.youtube.com/watch?v=RJDu233hO1k

L298N H-bridge PWM demo: https://www.youtube.com/watch?v=RJDu233hO1k

If you are not required to use PWM, another option is to use an RC speed controller, which operates similarly, but is controlled using SERVO commands.

http://www.ebay.com/itm/Trendy-Superb-10A-Brushed-ESC-Speed-Controller-with-Brake-for-1-24-RC-Car-Boat-/151877547269
 
Last edited:

tmfkam

Senior Member
The ULN2003A may need some additional circuitry to help it drive a 2A pump. The ULN2003A is only capable of passing 0.5A from each output so if your pump drew 2A the ULN2003A may not be happy.

Erco has recommended an ideal device, the L298. It is cheaper than my suggestion would have been, the TLE5206. The TLE5206 is smaller than the L298 as it has only a single output, but given there are loads of example circuits available for the L298, I'd be tempted to take Erco's advice.
 
Top