Hello, in this project I want to show you how to build a PWM generator with a Attiny processor. It is very easy to build and a nice project for starters as also for professionals. You will need some electronic knowledge and some knowledge about Arduino. It wont be to hard, because i already wrote a code for the Attiny, which you can use.

This PWM generator also works as servotester, it supports all kinds of servos, which dont need more then 5V


Operating voltage: 5V
Max PWM current: 1 A
PWM frequency (by now) : 500HZ , I am trying to make that changeable, if you know how, let me know :)
High effency
small size
small prize ;)


So lets get started!

Step 1: Material and Tools

The materials will cost less then 4$, also you need a Attiny programmer, i used an Arduino Uno!


2 Resistors (450 OHM)
1 Resistor (1 kOHM)
1 BC548 NPN Transistor (or equal)
1 BC516 PNP Transistor (or equal)
some Jumpers
1 Potentiometer (10 kOHM or higher)
serval kinds of terminals (picture 1)
some unisolated wire
1 eight pin IC holder
1 Attiny (25/45/85) I used the Attiny45
some PCB


soldering iron
a cutter knife
a pincer
a attiny programmer (Arduino Uno)
a breadbaord

<p>Jan,</p><p>I wrote my own pwm controller for a different microcontroller without using any built in pwm module using the following method:-</p><p>1. set up an interrupt to generate 50*255 times per second. - so that you can time every 1/50th of a second in 255 little time slices.</p><p>2. use a counter variable to count the interrupts as they happen and reset every 50 interrupts.</p><p>3. use another counter variable then to count from 1 to 255 interrrupts in that 1/50th of a second.</p><p>4. when the first counter resets to zero then set the pwm output to high. then, when the second counter hits your target width (by comparing its count with the pot input (suitably scaled to fit 1-255 range) you set the pwm output low.</p><p>so, every 1/50th of a second the output goes high and then after a period determined by your pot value it will go low until the end of that 1/50 of a second when it will go high again etc etc.</p><p>hope that helps.</p><p>cheers</p><p>jakers dad</p>
<p>Okay, interresting, i will try it :)</p><p>Thank you!!</p>
<p>Jan,</p><p>sorry, I mangled that explanation a bit. You actually only need one interrupt counter if you dont need to keep track of the number of seconds. see attached image which hopefully will make things clear.</p><p>of course you may need to take into account that position control only wants pulses between 1 and 2 mS long and not the full 20mS of the 50Hz period. My pwm was for speed control rather than position (and i was controlling acceleration so i needed the seconds info) but all the same it will work and is amazingly easy actually.</p><p>good luck</p><p>Jakers dad</p>
<p>Yea, Thank you! :)</p>
<p>code works great for my application at internal 1mhz driving a mosfet for a 12 volt motor</p>
<p>Is it possible to use the BC517 instead of BC516 transistor?<br> many Thanks</p>
<p>Hello,</p><p>In order to test servo, why do you need transistor on the signal output?</p>
<p>The idea is to also directly drive Motors and lamps with PWM :) you can remove it, when you only want to test servos!</p>
<p>sorry, what could be replacement for BC516? here i can not get it.</p>
<p>Theoretical any PNP Transistor :)</p>
<p>About the PWM frequency - if what you say is right, your &quot;Arduino&quot; is using the highest prescaler for the Timer1 (that one used to generate PWM on that pin). So to increase frequency, you just have to decrease prescaler. You might try macro from core_timers.h from tiny_core:<br>Timer1_ClockSelect( timer1_cs_t cs )<br>as timer1_cs_t cs you might try some of the values:<br>#define TIMER1_PRESCALER_VALUE_1 (1)</p><p>#define TIMER1_PRESCALER_VALUE_2 (2)</p><p>#define TIMER1_PRESCALER_VALUE_3 (4)</p><p>#define TIMER1_PRESCALER_VALUE_4 (8)</p><p>#define TIMER1_PRESCALER_VALUE_5 (16)</p><p>#define TIMER1_PRESCALER_VALUE_6 (32)</p><p>#define TIMER1_PRESCALER_VALUE_7 (64)</p><p>#define TIMER1_PRESCALER_VALUE_8 (128)</p><p>#define TIMER1_PRESCALER_VALUE_9 (256)</p><p>#define TIMER1_PRESCALER_VALUE_10 (512)</p><p>#define TIMER1_PRESCALER_VALUE_11 (1024)</p><p>#define TIMER1_PRESCALER_VALUE_12 (2048)</p><p>#define TIMER1_PRESCALER_VALUE_13 (4096)</p><p>#define TIMER1_PRESCALER_VALUE_14 (8192)</p><p>#define TIMER1_PRESCALER_VALUE_15 (16384)</p><p>Of course without #define's and values (I'm just lazy enough to copy-paste from .h file ;) )<br>If not, you might try to change the TCCR1 register value according to the attiny85 datasheet, page 89 and 90.<br>Hope this will help you. <br>P. S.<br>now probably tiny85 is using prescaler #15, so using #14 should increase frequency twice ;) to about 1kHz</p>
<p>Thank you, I will try it in the afternoon...</p><p>I already tried the TCCR1 register value, it didnt work good, but i will keep trying!</p><p>:)</p>
<p>Oh... I see, that you want to lower the frequency, not to increase... So that may be hard, because you'd need to increase prescaler, but it's impossible because of using the biggest now. But you're using Arduino Core, so why won't you just use Servo library?</p>
<p>Thats a good questions, by now I dont thought about it :D:D</p>
<p>measuring 5 times and delaying 10ms each time limits the PWM frequency -- measure ADC once like every half second or every second - and keep the pwm static for the rest of time. - this way you get rid of the unneeded delays, and can increase frequency. </p><p>and BC516 is only 500mA. (or I&acute;m missing something?)</p><p>otherwise - nice little gadget.. :)</p>
<p>Hi, it wont increase the frequency, it doesnt matter, how much the processor is occupied, it doesnt change the PWM frequency...</p><p>The PWM signal has a sperated timer(s)</p><p><a href="http://www.fairchildsemi.com/ds/BC/BC516.pdf" rel="nofollow">http://www.fairchildsemi.com/ds/BC/BC516.pdf</a></p><p>it says 1 A D: </p><p>Thank you for your input :)</p>
<p>nice work. looks good. you can make a servo tester with a 555 timer</p>
<p>Thank you, but with a NE555 it wouldnt work good, because the NE55 cant make a good PWM, it can just change the frequency, what can be used with some extra parts ( capacicators &amp; co ) to fake a pwm, try it with a oscilloscope... :P </p>

About This Instructable




More by Jan_Henrik:How to 3D-Print multicolored obejcts Photography How to make a diffusor for your LED Matrix 
Add instructable to: