Posts | Comments

Planet Arduino

Archive for the ‘PWM’ Category

Most projects are built on abstractions. After all, few of us can create our own wire, our own transistors, or our own integrated circuits. A few months ago, [Julian Ilett] found a problem using the Arduino library for PWM. Recently, he revisited the issue and used his own PWM code to fix the problem. You can watch the video below.

Of course, neither the Arduino library nor [Julian’s] code is actually producing PWM. The Atmel CPU’s hardware is doing the work. The Arduino library gives you a wrapper called analogWrite — especially handy if you are not using an Atmel CPU where the same abstraction will do the same work. The issue arose when [Julian] broke the abstraction to invert the PWM output.

The video does a good job of framing the issue. Setting the PWM hardware to zero still causes a one tick output to occur. That is, the actual count is the count you supply plus one. That’s great on the high end where 255 is treated as 256 out of 256. But at the low end, a zero counterintuitively gives you 1/256. The Arduino library authors elected to detect that edge case and just force the output pin to go low in that case. When inverted, however, the pin still goes low when it ought to go high. You can see the source code responsible, below.

pinMode(pin, OUTPUT);
if (val == 0)
  digitalWrite(pin, LOW);
else if (val == 255)
  digitalWrite(pin, HIGH);
{ ...

Oddly, the 255 case appears to be superfluous in the normal case but is also backward if you invert the output. In all fairness, the Arduino library doesn’t provide you a way to invert the output, so you’ve already broken the abstraction and that’s why this isn’t technically a bug in the library.

[Julian’s] code is quite simple. There’s initial set up of the TCCR1A and TCCR1B registers along with ICR1. The DDRB register sets the pin as an output. After that, writing to OCR1A and OCR1B set the PWM value. The video explains it all in great detail.

We’ve looked at PWM on FPGAs at least once, and that post gives some background on PWM in any application. We also have our own video from way back in 2011 about PWM.

Filed under: Arduino Hacks

How do you tell how much load is on a CPU? On a desktop or laptop, the OS usually has some kind of gadget to display the basics. On a microcontroller, though, you’ll have to roll your own CPU load meter with a few parts, some code, and a voltmeter.

We like [Dave Marples]’s simple approach to quantifying something as complex as CPU load. His technique relies on the fact that most embedded controllers are just looping endlessly waiting for something to do. By strategically placing commands that latch an output on while the CPU is busy and then turn it off again when idle, a PWM signal with a duty cycle proportional to the CPU load is created. A voltage divider then scales the maximum output to 1.0 volt, and a capacitor smooths out the signal so the load is represented by a value between 0 and 1 volt. How you display the load is your own choice; [Dave] just used a voltmeter, but anything from an LED strip to some kind of audio feedback would work too.

Still just looking for a load meter for your desktop? Take your pick: an LED matrixold-time meters, or even Dekatrons.

Filed under: Arduino Hacks
Aquesta pàgina ens explica com podem controlar la velocitat dels nostres motors fent ús de la funció analogWrite.S'utilitzen les sortides 10 i 11 de l'arduino UNO (pins PWM), connectades als pins enable del xip L293D dels dos motors del nostre robot.


Si voleu el muntatge en el simulador proteus (molt bona explicació) no us perdeu el vídeo:

Connor Nishijima has devised a neat trick to give the standard Arduino Tone() function 256 smooth volume levels using PWM at an ultrasonic frequency, without any extra components. This allows for programmatic control of square waves with nothing other than a speaker connected to an Arduino Uno.

Normally to simulate an analog voltage with a digital-only pin of a microcontroller you’d use Pulse Width Modulation. This works great for LEDs because your eyes can’t the 490 / 976Hz flicker of the standard analogWrite() function. But for audio things are a bit more difficult. Because your ears can easily detect frequencies between 20 – 20,000Hz, any PWM with a frequency in this range is out.

Luckily, the ATmega328P allows you to change the clock prescalers for ultrasonic PWM! We need to use Timer0, because it can drive PWM at a max frequency of 62,500Hz, which even if you cut that in half would still be above your hearing range. Now that we have ultrasonic PWM on Pins 5 & 6, we configure Timer1 to fire an Interrupt Service Routine at a rate of “desired frequency” * 2.

Finally, inside the Timer1 ISR routine, we incorporate our volume trick. Instead of digitalWrite()’ing the pin HIGH and LOW like the normal Tone() function does, we analogWrite() “HIGH” with our volume value (0 – 255) and analogWrite(0) for “LOW”. Because of how fast the PWM is running, the user doesn’t hear the 62.5KHz PWM frequency, and instead perceives a 50% percent duty cycle as a speaker driven with only 2.5 volts! While a few volume levels do produce subtle artifacts to the sound, it mostly delivers quality 8-bit volume control to replace the standard Tone() function.

When all is said and done, you’ll be able to customize your project with unique loudness as you play anything from the iconic Nintendo sound to R2-D2’s beeps and bops. In Nishijima’s case, he developed this Arduino volume-control scheme to make an incessant, inconsistent artificial cricket to hide in a friend’s vent for the next few months… You can read more on its page, as well as find documentation and ready-to-use example sketches GitHub.


Simple Infrared PWM on Arduino

arduino, IR, PWM Commenti disabilitati su Simple Infrared PWM on Arduino 



We are often asked on discussion boards, about conflicts between IRremote or IRLib and other Arduino Libraries. In this post, we present a sketch for ‘Simple Infrared PWM on Arduino’. This is the first part in a 3 part series of posts. Part 1 shows how to generate the simple Infrared carrier frequency on Arduino, using any available IO pin and without conflicting with other libraries. Part 2 will show how to send a RAW infrared signal using this approach and Part 3 will show how to send a common NEC signal from the binary or HEX value.

Simple Infrared PWM on Arduino – [Link]


Arduino solar charge controller and energy monitor

arduino, charging, Energy, monitor, PWM, solar, Test/Measurements Commenti disabilitati su Arduino solar charge controller and energy monitor 


by deba168 @

One year ago, I began building my own solar system to provide power for my village house.Initially I made a LM317 based charge controller and an Energy meter for monitoring the system.Finally I made PWM charge controller.In April-2014 I posted my PWM solar charge controller designs on the web,it became very popular. Lots of people all over the world have built their own. So many students have made it for their college project by taking help from me.I got several mails every day from people with questions regarding hardware and software modification for different rated solar panel and battery. A very large percentage of the emails are regarding the modification of charge controller for a 12Volt solar system.

Arduino solar charge controller and energy monitor - [Link]


On Arduino due PWM frequency

arduino, ATmega328P, PWM Commenti disabilitati su On Arduino due PWM frequency 


Kerry D. Wong writes:

I just got myself a couple of Arduino Due boards. While they were released almost two years ago, I have not really got a chance to look at these until quite recently. Arduino Due is based on Atmel’s ATSAM3x8E 32-bit ARM Cortext-M3 processor. The processor core runs at 84 MHz, which is significantly faster than its 8-bit AVR counterpart ATmega328p which runs at 16 MHz. For an ATmega328p, the highest achievable PWM frequency is 8Mhz (square wave), so we should be able to generate much higher frequency signals on an Arduino Due. But how high can we go? Let’s find out.


On Arduino due PWM frequency - [Link]


Meter clock: keeping “current” time

analog, arduino, clock, PWM, timer Commenti disabilitati su Meter clock: keeping “current” time 


Meter clock: keeping “current” time. Read more about the clock:

I’ve seen a few meter clocks in my travels of the web, and I love the idea. A few days ago, I decided that I must have one of my own. Such began the “How to do it” pondering cycle. I had seen builds where the face plate of the meter is replaced. This works, but I wanted to try and find a way to do it without modifying the meter, if possible. After some more ponderation, I came up with what I think is a serviceable idea.

I came across this style of milliamp meter on Amazon. They’re not quite 0-60 mA, but the 0-100 mA (a 0-20mA meter for the hours) is close enough. And they were cheap. So yay.

Part of my requirements were that the clock run off of an Arduino Pro Mini I had lying around, and with minimal additional parts. In order to drive the meters with some degree of precision, I would use the PWM pins to vary the effective voltage across a resistor in series with the meter. This would, by the grace of Ohm’s Law, induce a current that, based on the PWM duty cycle, would be scaled in such a way as to move the needle on the meter to the corresponding hour, minute, or second.

One minor issue came up in the form of the max current the GPIO pins on the ATMega328 chip can source/sink. The pins can source/sink a maximum of 40mA, a bit far from the 60mA needed for the minutes and seconds meters. Enter the transistor.

Using a simple NPN transistor switch circuit, I was able to provide the current for the minute and second meters from the 5V supply. The PWM signals switch the respective transistors on and off, effectively varying the voltage across the resistors in series with the meters.

The resistor between 5V and the meter is actually 2 1/4 watt 100 Ohm resistors in parallel for an effective resistance of 50 Ohms. The two in parallel was necessary as 5V x 0.06A = 0.3W (more than 0.25 that a single 1/4W resistor can handle safely).


Meter clock: keeping “current” time - [Link]


Galileo now runs Windows

arduino, Galileo, intel, PWM Commenti disabilitati su Galileo now runs Windows 



Microsoft have released a non-commercial version of Windows based on Windows 8.1 to run on the Intel Galileo development board. A spokesperson for Microsoft said “This preview Windows image is another opportunity for makers and developers to create, generate new ideas and provide feedback to help Microsoft continue making Windows even better on this class of device”.

The board and OS are part of the Windows Developer Program for the IoT (Internet of Things) which Microsoft hopes will encourage developers of Internet-connected devices to experiment with Windows platforms. The Galileo kits include the standard Arduino Wiring API and a subset of the Win32 API. At the moment Linux is the OS of choice among makers and for the next generation of devices but Microsoft hopes to break that dominance. Intel released an update to the original Galileo Gen 1 board earlier this month which features an improved control of its PWM (Pulse Width Modulated) output signals to make the board better suited for the management of 3D printers and robotic applications. Galileo Gen 2 can also be powered from the Ethernet port which the earlier Gen 1 version did not allow.

Galileo now runs Windows - [Link]


How to make a simple wav player by using Arduino

arduino, PCM, PWM, WAV Commenti disabilitati su How to make a simple wav player by using Arduino 


Integrated with the homemade low-pass filter, this Arduino-based simple WAV player is to send out PWM signal generated by UNO,

then through the low-pass filter and make the PCM data stored in the flash of UNO into sounds. Basically, the player cannot be regarded

as a pure WAV playback, because by extracting the data from the WAV file and storing it in an array format in UNO, this tutorial is for reference.

You can make SD card based WAV player by referring to this idea.

How to make a simple wav player by using Arduino - [Link]

  • Newsletter

    Sign up for the PlanetArduino Newsletter, which delivers the most popular articles via e-mail to your inbox every week. Just fill in the information below and submit.

  • Like Us on Facebook