New Products Flash Sale
Thread Tools
Old Jan 13, 2015, 04:17 PM
wvmorgan is offline
Find More Posts by wvmorgan
Registered User
United States, NY, Ithaca
Joined Mar 2014
27 Posts
Discussion
DIY ATtiny13 Micro brushed ESC, Choose your own current rating!

I am building a brushed ESC that can be configured easily to have any current rating that you desire just by choosing a different mosfet!

I currently have it working with forward only, I would like to make it reverse capable as well. I have a few ideas on how to do it but I will gladly take suggestions.
I will post the source code file(written in C) and any updateds along with any working HEX files.

Hobbyking sells a micro 1S Lipo 3amp brushed ESC which I have and it works nicely. This ESC uses a tiny13 and a mosfet. I have drawn a schematic for the HK ESC and will attach it to this post. This is what I am basing my initial efforts off of.
Here is the link to the ESC: http://hobbyking.com/hobbyking/store...ro_Motors.html

From the schematic I have gathered that the motor PWM is probably using the first hardware PWM unit in the tiny13(pin 5, OC0A) and the servo PWM input is either using the PCINT on pin 2 or maybe the ADC but probably it is just a regular IO port and everything is done in the code.

I am currently using PORTB3(pin2) for the input and OC0A(pin5) for the PWM output to the mosfet.

Logic level mosfets must be used if you are driving them directly with the microcontroller, otherwise they will not turn on fully!!!
Explanation: Regular N-ch mosfets turn on all the way when the gate reaches 10 volts, logic level mosfets turn on all the way with between 2 and 5 volts on the gate depending on the mosfet. ATtiny and ATmega microcontrollers can only put out up to 5 volts on their pins and less if their supply voltage is lower than 5 volts like if you are using a 1-cell lipo, so use a logic level mosfet.

Here is the current code, forward only:

Code:
/*
 * T13_ESC1.c
 *
 * Created: 1/12/2015 7:59:08 PM
 *  Author: Will
 */ 

#define F_CPU 9600000 // internal clock @ 9.6MHz
#include <avr/io.h>
#include <util/delay.h>

void PWM_setup(void)
{
	
	TCCR0A |= ((1 << COM0A1) | (0 << COM0A0) // COM0A1 - COM0A0 (Clear OC0A on Compare Match when up-counting. Set OC0A on Compare Match when down-counting.)
	| (0 << WGM01) | (1 << WGM00)); // WGM01 - WGM00 (set Phase Correct PWM Mode)
	
	TCCR0B |= (1 << CS01) | (1 << CS00); //clk/8 prescaler 
}

void PWM_write(int val)
{
	OCR0A = val; //This value sets the duty cycle 0 to 255 (8-bit timer)
}

int main(void)
{ 
	DDRB = 0b00000001; //PortB0 = output
	int Pulslen = 0;
	int TimeOut = 0;
	PWM_setup();
	
	while(1)
	{
		while(PINB & 0b00001000)  // while the servoPWM input is high
		{
			Pulslen=Pulslen+1; // increment
			_delay_us(8); //resolution setting, may require further adjustment
		}	
	
		if((Pulslen > 114) * (Pulslen < 118)) //throttle must be low initially
		{	
		
			while(1)
			{
			
				while(PINB & 0b00001000)  // while the servoPWM input is high
				{
					Pulslen=Pulslen+1; // increment
					_delay_us(8); //resolution setting, may require further adjustment
				}
		
				if (TimeOut>20000) //this will shut off the motor if the RX signal is lost
				{
					Pulslen = 115;
				}
			
				if (Pulslen > 114)  // ignore false triggers and only set the PWM when a pulse is received 
				{
					Pulslen = ((Pulslen-115)*2);
			
					//if(Pulslen<0){Pulslen=0;} //just in case there is a negative 
			
					if (Pulslen<5) //lower threshold, produces a dead-band  
					{Pulslen = 0;}
			
					if (Pulslen>220) //Full power after crossing a threshold determined by trial and error
					{Pulslen = 255;}
				
					PWM_write(Pulslen); //sets the PWM duty cycle
		
					TimeOut = 0;  //resets the timeout counter
			
				}
		
				if (Pulslen == 0) //counts every time the loop completes and a pulse is not registered
				{
					TimeOut+=1;
				}
		
				Pulslen = 0; // resets the variable for the next pulse measurement and starts the timeout counter	
		 
			}
		}
		else
		{Pulslen = 0;}
		
	}
}


(cheap)Mosfets that should work properly: (can be found on ebay by searching the part number, most are under a dollar each if you get a few of them)

SO-8:

FDS6570A 15A, 20V (would be very good for a single cell ESC as if has lower on-resistance at single cell voltages)

FDS6680A 12.5 A, 30 V

IRF7821 13.6A, 30V

FDS8813 18.5A, 30V


TO-252 (DPAK):

FDD8447L 50A, 40V

IRL3502S 110A, 20V




A note on mosfet ratings:

Keep in mind when choosing a mosfet that the current ratings above, which are also found in the datasheets, are the max values at room temperature. When used in the ESC they will probably get hot and that rating will go down.

You should expect to over rate the mosfet current by 4 times. For example, if you need a 10 amp ESC, you should use a mosfet that is at least rated for 40 amps.

The voltage rating should also be around double the max voltage that you plan to use with the ESC, so if you want to use a 2-cell lipo(8.4V at full charge) you should use a mosfet rated for at least 16 volts. The voltage shouldn't be an issue though as most mosfets I have listed are at least 30V.

File Info:
The .txt files are C code. RCGroups will not let me post them with the .c extension so just save them as .c instead of .txt after you download them.

The HEX files should be ready to go, I won't post any HEX files that don't work or have major issues that I know of. If you have a problem please let me know and I'll try to correct it as soon as I get a chance. Always test an ESC without a prop on the motor before hand to make sure that it isn't going to start spinning before you tell it to.

The code will now shut off the motor if the signal is lost and the throttle must be put down all the way after you power up the ESC to enable the motor.



Any suggestions, help or contributions are welcome.

If you have a specific requirement that I am not working on, let me know as I might be able to help. (I don't know much in AVR-C so that is a maybe but you never know)




Disclaimer:
I am not responsible if you hurt, damage, maim, kill, or otherwise destroy any person, animal, thing or yourself. If you built an ESC from the information here you have taken the responsibility into your own hands and I would expect that you have common sense and know the safe practices for ESC's and those spinning sharp things that we call propellers. So, in other words, if you chop off a finger because you forgot to set your throttle trim properly after building a massive ESC for a 1:5th scale plane, don't come crying to me!



People who helped make it work:

ruzam
noobee
bselman

(sorry if I missed you, nothing personal, just bad memory)





Will
wvmorgan is offline Find More Posts by wvmorgan
Last edited by wvmorgan; Jan 16, 2015 at 12:53 PM.
Reply With Quote
Sign up now
to remove ads between posts
Old Jan 13, 2015, 06:17 PM
geoline4 is offline
Find More Posts by geoline4
old first time beginner
Joined Mar 2009
7 Posts
Interesting to see this post. OK so if I go get a $2.00 Attiny13 and $1.00 FET etc. What other
equipment do I need to program the "tiny" ? Assuming you will post the final program should
you get the help needed, will programing hardware cost more than the HK unit?
Anyway fun to see this through.
geoline4 is offline Find More Posts by geoline4
Reply With Quote
Old Jan 13, 2015, 06:43 PM
coriolan is online now
Find More Posts by coriolan
"Unnecessary Necessity"
coriolan's Avatar
Canada
Joined Sep 2006
6,587 Posts
Interesting project, you might have more feedback in the DIY electronics forum:
http://www.rcgroups.com/diy-electronics-199/
There was several threads about that HK esc (I have one which works well):
http://www.rcgroups.com/forums/showthread.php?t=2217043
http://www.rcgroups.com/forums/showt...ht=brushed+esc
Will follow your thread with interest!
coriolan is online now Find More Posts by coriolan
Reply With Quote
Old Jan 13, 2015, 07:59 PM
wvmorgan is offline
Find More Posts by wvmorgan
Registered User
United States, NY, Ithaca
Joined Mar 2014
27 Posts
geoline4 -
The only other thing that you would need to program the tiny13 is a programmer, you can get a USBasp programmer on eBay for less than 5 dollars, this is what I use.
Here is a link to one on eBay: http://www.ebay.com/itm/USBASP-USBIS...item484ba76ee2

The other part is that you would be able to use the same tiny13 with different size mosfets so you can have anything from a 1amp ESC to a 40amp or so ESC using the same code and little change in cost. i.e. you can use something like an IRFZ44N mosfet that can handle 36 amps at 100C or you can use a small so-8 package mosfet for just a couple amps. Both of which would probably cost about a dollar(or less if you know where to look).

coriolan -
Should I just repost it over in the DIY electronics forum and leave it here as well or move it to that forum? (New to forums, not really sure)
Thanks for the links.

-Will
wvmorgan is offline Find More Posts by wvmorgan
Reply With Quote
Old Jan 13, 2015, 09:14 PM
coriolan is online now
Find More Posts by coriolan
"Unnecessary Necessity"
coriolan's Avatar
Canada
Joined Sep 2006
6,587 Posts
Would be best to move it rather than have two different threads on the same subject in different forums. Funny that you mentioned the IRFZ44N Mosfet, used several for my DIY brushed ESC for boats during my early fast electric days!
coriolan is online now Find More Posts by coriolan
Reply With Quote
Old Jan 13, 2015, 09:40 PM
wvmorgan is offline
Find More Posts by wvmorgan
Registered User
United States, NY, Ithaca
Joined Mar 2014
27 Posts
How do I go about moving it, I don't see the option to do so?

That is a funny coincidence! They are just a type that I have on hand that can handle a decent bit of current. Did they work well for your boats? Also, do you happen to have a circuit? I would like to use the micro but if that fails I could always use an analog circuit at least for the larger ESC that I currently need.
wvmorgan is offline Find More Posts by wvmorgan
Reply With Quote
Old Jan 13, 2015, 10:08 PM
coriolan is online now
Find More Posts by coriolan
"Unnecessary Necessity"
coriolan's Avatar
Canada
Joined Sep 2006
6,587 Posts
I think you need to ask the moderator to move it. I was using the Z44 because of low price and low on resistance, using up to 6(TO-220) in parallel mounted on a water cooled heat sink for large brushed speed-700 motors. The separate driving circuit was just analog using a variation of this circuit(without the BEC):
http://www.stefanv.com/rcstuff/escbec.htm
It worked well and I made many before going brushless+Lipo!
coriolan is online now Find More Posts by coriolan
Reply With Quote
Old Jan 14, 2015, 12:42 PM
ruzam is online now
Find More Posts by ruzam
Go small or go home
ruzam's Avatar
Canada, SK, Regina
Joined Mar 2008
1,545 Posts
I don't know anything about the ATtiny13A. A quick google tells me you can use interrupts (or at least one interrupt). I would probably try to make use of that for reading the servo input.

Some observations:

You're using a continuous loop to read the servo input, which will be dependent on the chip clock rate which is very likely dependent on chip voltage. As the battery drains, this could change the loop timing and throw off your servo read pulse timings. So 'center stick' doesn't stay center through the entire range of the battery. I'm always looking for reversible ESCs, so that's a big deal for me to have an accurate center stick value throug the entire run, maybe not so much for a simple throttle ESC. At some later point you may want to try to compensate for changing voltage/temperature if the chip has any kind of internal indicators you can use.

Your main loop is fairly straight forward, but you include the Pulse_length calculation in it. To make things worse the Pulse_length calculation includes floating point values (2.55). To perform floating point arithmetic in a tiny chip like this requires a significant amount of library code, which the compiler is building in for you (it may not even be doing what you think). The calculation itself may vary in the amount of time it takes, depending on the value of the numbers being multiplied. So it's probably your biggest source of random timing.

You only 'need' to calculate the Pulse_length once when the input signal goes low again. I would pull it out of the pin high loop. Check the Pulslen value after the loop. If it's not zero, then calculate Pulse_length and set the PWM_write() once. Then the entire calculation will happen outside the timing.

Be careful that your Pulslen isn't over-flowing. Be careful that your Pulse_length calculation isn't going negative or over-flowing. I would try very hard to get rid of the floating point. Try to play with the _delay_us() delay to get the Pulslen closer to a usable Pulse_length without having to do any more calculation. You might be able to skip the extra Pulse_length variable all together.

Be careful of false triggers. You might want to ignore Pulslen if it's less than some minimum value.

And when that's all working, don't forget to build in a startup safety that ignores throttle input until it goes do a minimum safe value first




Code:
while(1)
{
		
	while(PINB==0b00001000)  // while the servoPWM input is high
	{
		 Pulslen=Pulslen+1; // increment
		_delay_us(25);  // fudge this number for better timing
	}
			
	if (Pulslen > 100)  // ignore false triggers
        {
		Pulslen -= 100;
 	        PWM_write(Pulslen);
	}

	Pulslen = 0;
 
}
ruzam is online now Find More Posts by ruzam
Reply With Quote
Old Jan 14, 2015, 02:10 PM
nemoskull is offline
Find More Posts by nemoskull
Pilot, Co-pilot, Navagator
nemoskull's Avatar
United States, AZ, Yuma
Joined Sep 2009
2,808 Posts
um okay. first off, move chips to a atytiny 85 or 45. then use arduino IDE. (your need to do some fiddleing with the arduino IDE to get it to work with the tiny core, ive done it on the 85 and 2313 chips.)

use the pulsein command to get the servo sig in in a uS value. then pwm the outputs. i dunno if the TimerOne.h library works with the tiny core, but on the atmega 328p chip you can set the pwm cycle time from 10 us to about 8 minutes using the TimerOne.h library. or you can do the PWM in software.
on the mega, the TimerOne.h works only on timer1 hardware timer, so pins 9 and 10 on the mega328.

just some ideas.

edit: and you will need a voltage regualtor, the brushed motors are too nosisy and will cuase a reset. also, disabling reset(meaning, no more code uploads) might be a good idea, or tie a resistor or capacitor(forget whitch one) from VCC to RESET.
nemoskull is offline Find More Posts by nemoskull
Reply With Quote
Old Jan 14, 2015, 03:11 PM
wvmorgan is offline
Find More Posts by wvmorgan
Registered User
United States, NY, Ithaca
Joined Mar 2014
27 Posts
ruzam

Thank you. This is exactly the kind of thing that I need help with as I am used to high level computer programming where things like floating point numbers don't really matter because a modern computer is fast enough.

As far as the clock frequency variations go I could probably setup an external clock in order to make reverse useable. I don't know if this would be required though, need to test it to find out as I didn't see any internal clock stability specs in the datasheet. It is running at 9.6MHz though so small changes in freq shouldn't mess up timing too badly being as any timing thing will be divided down considerably. Could always just make the dead band larger to compensate for any drift depending on how bad it is. (I would also like to make this reversible, but need to get the simple version to work first)

The Pulse_length calculation was in the pin high loop only after messing around with its placement, that was simply the last place that I checked before posting this, I realize that it will cause some issues now.

I will try your code suggestion in a minute. Also, I was planning on making a safety of some sort but would need to get the servo pulse length first. We don't need any runaway models!

Will
wvmorgan is offline Find More Posts by wvmorgan
Reply With Quote
Old Jan 14, 2015, 03:23 PM
wvmorgan is offline
Find More Posts by wvmorgan
Registered User
United States, NY, Ithaca
Joined Mar 2014
27 Posts
nemoskull

I appreciate the help, I may start working with a more capable micro if this becomes too cumbersome with limited hardware. I would probably avoid arduino though because it makes the programming process much more difficult. With a simple AVR hex file I can write it to the micro in about a minute, I am fairly certain that this would not be the case with loading the arduino IDE and then loading the actual code.

I want this project to be as easy as possible for anyone who wants a brushed ESC even with minimal programming knowledge to be able to make one on there own.

Switching to the tiny85 may be a good idea though as it has two timers. I want to see what can be done with the tiny13 first though.

The circuit that I posted, it is the actual circuit of the HK BR3A ESC. That ESC works(the older batch anyway) and has no issues that I can see in my experience so clearly a voltage regulator is not required. A pullup resistor on the reset pin is probably a good "just in case" thing though.

Will
wvmorgan is offline Find More Posts by wvmorgan
Reply With Quote
Old Jan 14, 2015, 04:05 PM
ruzam is online now
Find More Posts by ruzam
Go small or go home
ruzam's Avatar
Canada, SK, Regina
Joined Mar 2008
1,545 Posts
Sweet! I found a full how-to RaspberryPi compiler/programmer for the ATtiny13. Nothing more than a few resisters and you're good to go. When the shipping boat lands, I can join in with the hands on. But anyway...

The 45/85 do have another timer, and an on board temperature sensor, and more programming memory. All of which are useful, but I don't see how any of them are required in this use.

Digging into the data sheets I see that you can set interrupts for both rising and falling pin level transistions. For reading servo input I would start by putting that pin on an interrupt. I'm still not quite clear on how the timer interracts (is used) with the PWM. You should be able to both read the clock value from interrupts and operate the PWM to get accurate input pulse timing using only a single clock. Or simply use the main loop as a clock counter.

Make sure you've taken care of the watch dog so it's not resetting your main loop. You can also set the watch dog to cause an interrupt, making it another timing source. I think the watchdog timer operates independantly of the clock.

The ADC can use either a built in 1.1V internal reference or Vcc. I think you might be able to wire up an ADC pin to determine a reasonable value for the chip voltage. That would get you a low voltage cut off and maybe some clock frequency adjustment.
ruzam is online now Find More Posts by ruzam
Reply With Quote
Old Jan 14, 2015, 04:56 PM
wvmorgan is offline
Find More Posts by wvmorgan
Registered User
United States, NY, Ithaca
Joined Mar 2014
27 Posts
It works!!! I'll edit my first post to reflect the progress in a bit.

Turns out that the tiny13 is fine for a forward only ESC. Idk about reverse, we'll have to see.

Now I need to work on some safety things that way it isn't a dangerous ESC(we don't need any missing fingers if someone decides to go big with this thing on anything with a propeller).

Will
wvmorgan is offline Find More Posts by wvmorgan
Last edited by wvmorgan; Jan 14, 2015 at 05:39 PM.
Reply With Quote
Old Jan 14, 2015, 05:50 PM
bselman is offline
Find More Posts by bselman
Registered User
bselman's Avatar
sw missouri
Joined Sep 2001
412 Posts
In my previous post I suggested testing for rollover. You should also test for under value. IE

if(Pulslen>200) Pulslen=200;
else if(Pulslen<100) Pulselen=100;

then adjust for the PWM:

Pulse_length = ((Pulslen-100)*2.55);

And floating point math is not a good idea in these small microprocessors.

Bob
bselman is offline Find More Posts by bselman
Reply With Quote
Old Jan 14, 2015, 06:07 PM
wvmorgan is offline
Find More Posts by wvmorgan
Registered User
United States, NY, Ithaca
Joined Mar 2014
27 Posts
I did away with the floating point math and set up a filter for the under value condition( the "if (Pulslen > 100) // ignore false triggers" line up in the code)

I haven't setup an over value condition yet though.

Seems to be working smoothly, going to test it with a motor in a few minutes.

EDIT: I tested it with several different motors from a small 8.5mm pager up to a huge coffee can sized motor that I have sitting around, worked without an issue once I put some capacitors and a diode in the circuit to reduce interference. I don't really know how to make it only enable the output if the throttle starts from zero though... It currently just starts up as soon as it sees a signal and if the signal disappears it just stays at whatever speed it was at last so I also need to make a timeout that will shut off the output if the servo signal is gone for a second or so.

Will
wvmorgan is offline Find More Posts by wvmorgan
Last edited by wvmorgan; Jan 15, 2015 at 05:11 AM.
Reply With Quote


Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
For Sale Losi: 1/24 Micro Rally brushed, with BL motor and ESC. tware Cars - Cars and Parts (FS/W) 3 Feb 14, 2015 12:14 AM
Discussion Help with making a diy bldc esc Grimmjow619 Multirotor Power Systems 3 Oct 23, 2014 05:24 PM
Discussion 2 brushed motors with 1 ESC & 2 micro servos ejohn Power Systems 1 May 09, 2014 11:31 AM
Discussion Brushed esc problem NEED HELP! Somethin' Extra Off-road Cars 11 Feb 28, 2014 11:25 AM
DIY Brushed Controller W/ Program Listing & Hex Code MAVA DIY Electronics 1 Feb 26, 2004 11:53 PM