Author Topic: Basics of PWM $50 robot  (Read 2353 times)

0 Members and 1 Guest are viewing this topic.

Offline greywanderer012345Topic starter

  • Robot Overlord
  • ****
  • Posts: 133
  • Helpful? 2
Basics of PWM $50 robot
« on: January 22, 2011, 06:32:52 PM »
I'm trying to learn the basics of using pwm. The Tutorial seems to suggest to use pwm on PORTB. Can pwm be used on PORTD, because I've built the $50 robot circuit. I'm wanting to learn about pwm by using it to control my servos. Can anyone help me out? I'm using the atmega328p. Thanks.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Basics of PWM $50 robot
« Reply #1 on: January 23, 2011, 02:50:00 PM »
Hardware PWM for servos is best done by a 16 bit timer. The ATMega8, ATMega168 and ATMega328P only have one 16 bit timer: Timer 1. Since the PWM is done in hardware then the output is done to fixed pins on Port B.

Writing your own PWM software (or using WebbotLib, say) allows you to output PWM to (almost) any pin - ie increased flexibility at the expense of extra code (and CPU usage).

Why does it need a 16 bit timer?
Well servos require a pulse every 20ms and the 'position' pulse is normally 1ms to 2ms long (ie a range of 1ms out of 20ms so about 1/20th of the total). An 8 bit timer would only give (2^8 = 256) 256 time intervals for 20ms or 12 (256/20) different position settings for the servo. Most servos can cope with much better positional accuracy than just steps across their entire range.


Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline greywanderer012345Topic starter

  • Robot Overlord
  • ****
  • Posts: 133
  • Helpful? 2
Re: Basics of PWM $50 robot
« Reply #2 on: January 23, 2011, 04:53:03 PM »
Thanks. That clears up a few things. Can anyone tell me or link me to how to use/access the timer? I'm using the atmega328p and the latest versions of avr studio and winavr. Right now, I have fiugred out how to program my servos to a certain position(not modified yet) using a method similar to the servo_left() etc functions in admin's SoR_Utils.h, but I'm using a for() loop as a delay and have figured out that a pulse of 30-135 cycles of that loop followed by a delay of so many loops. I didn't know that the pulse should be sent every 20ms before. I would rather use the timer and a delay out of good practice. I don't know how to access the timer or how to make the mcu "sleep" or wait. Can anyone clear any of this up for me? Thanks, again.

Offline don bunot

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
    • Skype - rc.donrey.vargas
Re: Basics of PWM $50 robot
« Reply #3 on: February 16, 2011, 05:08:51 AM »
hi have you guys tried using a PWM program on DC MOTOR like TAMIYA TWIN MOTORS, using microcontrollers?

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Basics of PWM $50 robot
« Reply #4 on: February 16, 2011, 12:55:55 PM »
Absolutely - but unlike servos you MUST use a hardware motor controller to drive the motors as a microcontroller doesn't have the power to drive a motor directly
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Basics of PWM $50 robot
« Reply #5 on: February 16, 2011, 06:51:27 PM »
Thanks. That clears up a few things. Can anyone tell me or link me to how to use/access the timer? I'm using the atmega328p and the latest versions of avr studio and winavr.

There are a number of good tutorials on AVR Freaks
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=search&mode=results

Not to mention a very good tutorial here:
http://www.societyofrobots.com/member_tutorials/node/228

Also, the Atmel site has some application notes too. They've changed their site around though, so I don't know where things are again yet. Search for timer or pwm at http://www.atmel.com.

Joe