AdSense

Saturday, 25 May 2013

ATMega - Control an ESC

(Deutsche Version) I recently bought an brushless motor and an electronic speed controller. In this post, I want to explain what kind of signal has to be applied to the ESC to move the motor.

An ESC is constructed like this: 3 cables for the motor, 2 cables the battery and 3 cables for the PWM signal. The motor cables can be connected to any motor cable, if the motor spins into the wrong direction, simply switch two cables.

PWM is the shortcut for pulse width modulation. This means, for a several amount of time, there is a high signal and then a low signal. This is often used to set the brightness of LEDs. At an ATMega, a counter counts to 255. If i set my PWM channel to some value, e.g. 150, the output will be high from 1 to 150 and then low until 255.

The three cables of the esc are black (connected to ground), red (not connected) and white (connected to the PWM output). To calibrate the motor, a sequence has to be executed:
a) Maximum power signal (PWM channel set to  250)
b) Minimum power signal (PWM channel set to 150)
Now the motor speed will be linear dependant on the length of the PWM, 150 equals no power and 250 equals maximum power. Here is the code for an ATMega8:


#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>

int main( void )
{
  DDRB=0xFF; //Define all PORTB-channels as output
  OCR2=250; //250 of 255, nearly all time high
  TCCR2=0x6D; //Frequency set to a bit more than 50 Hz

  _delay_ms(10000);//Wait until the motor starts beeping
  OCR2 = 150;((//150 of 255, this is the no-power signal
  _delay_ms(10000);
//wait until the motor is calibrated

  //Now the ESC is calibrated and can be used:
  //OCR2 = 150: Minimum power
  //OCR2 = 250: Maximum power

  OCR2 = 200;//Set to 50% power
  while (1)
  {}
}

1 comment:

  1. Nice post , but you should be more descriptive about setting the registers values.

    ReplyDelete