Tool Writes Your PWM Code For You

The good thing about computers is they do your work for you, right? If you are a programmer, that doesn’t always seem to be a true statement. [Runtimemicro] has the answer, at least if you are writing PWM code for the Arduino. Their free application lets you set a few parameters, visually see the results, and then generates code for you. You can see a video of the tool in operation, below.

According to their site, the tool works for timers 1 through 5 on an Arduino Nano, Uno, or Mega2560. The app appears to work on Windows, but it doesn’t look like it would have any trouble running under Wine on other platforms.

There are only a few inputs: the clock speed, which timer you want to use, and the mode. You also have to specify the frequency in Hz or the period in milliseconds. You can also select a few options, including if you want interrupt code generated.

Once the timer shows up in the graphical display, you can adjust some sliders to get the exact PWM duty cycle you want. Of course, you can also skip the PWM code and just use the timer interrupts for timing.

It isn’t that the timer code or PWM isn’t workable without a tool. But then again, you don’t really need an assembler or a compiler — it just makes things easier. There are a few nuances, though. If you want to dig through the generated code, you might find [Jack’s] video interesting.

40 thoughts on “Tool Writes Your PWM Code For You

    1. Not for some folk’s needs. I’ve used arduinos in many projects and have next to no interest in learning to code if there’s an existing codebase or tool that let’s ke avoid it.

      1. You’re not a “full stack” engineer unless you are assembling your programs by hand and entering them into the computer from the front panel switches. Otherwise you are “cheating”

    2. There are many programs that calculate the register values for various timers on microcontrollers, clock generators, etc. Most of them display the values in boxes labeled with the register names. This one just displays it in the form of code that anyone could write given the register names and values. It’s not replacing knowing how to code, just saving some typing.

    3. “I thought the purpose of the Arduino was to learn how to code.”

      Here is the actual mission statement from the web page:

      “Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s intended for anyone making interactive projects.”

      Software like this fits quite well into the “easy to use” part of the mission statement.

    4. coding natively in atmel studio opens up many more possibilities than just programming with the arduino IDE. But yes this kind of does. But it seems like more of a tool for people who don’t use arduino to program AVR’s.

    5. >I thought the purpose of the Arduino was to learn how to code.

      No. Coding inevitably involves some understanding of the underlying platform and hardware because you have to understand the machine to understand what you’re doing and what -it- is doing – which the Arduino tries to abstract away and hide.

      In actually teaching students, we’ve found the Arduino problematic because it’s designed to skip a lot of the fundamentals, which then causes the students to fail in their assignments without ever understanding why and making it hard for the teachers to explain why because they students are cutting in from the sidelines without any background in even the most basic electronics or programming. For example, trying to produce a motion ramp for a servo motor by counting up a value once every millisecond and putting it out by Servo.Write(); What’s the problem here? Well, the servo signal has a period of 20 milliseconds, so they’re actually sending every 20th value they’re computing. Try to explain that to a person who’s never even seen a servo signal on an oscilloscope, or used an oscilloscope for that matter, or a soldering iron, or who measures resistors with a multimeter by pinching both leads with their fingers…

      This tool is actually teaching you to code by showing how to change the register values in an AVR and explaining what they do. This is useful. Arduino isn’t. The point of the Arduino is to sell Arduino boards and shields, so they provide you with libraries of functions as a buffer between you and actually learning how the hardware works, how electronics works, how embedded systems work… so you’d be locked in their ecosystem and couldn’t actually start a project without first loading up the Arduino bootloader.

      1. To explain it in other words: one constant in life is laziness. If the students don’t need to look up how a servo motor works and how it is controlled, they won’t. They’ll order “a servo” from Adafruit without understanding what it is, and connect it up to an Arduino board by some example given for a micro servo without understanding why it is so, use the default example “sketch” as a basis without understanding how it works, and then wonder why the servo doesn’t turn, or if it turns, why it behaves weirdly and doesn’t do what they think it should be doing, why their Arduino board is glitching out etc. etc…

        Gee. Maybe it’s because the Arduino isn’t designed to power your servo, which you would have noticed if you didn’t just copy/paste it all from google. You’re lucky you didn’t break the Arduino board in the first place, but sure enough it’s “tolerant” of such overloading and will just behave weirdly.

        An Arduino is kinda-sorta useful if you already know approximately what you’re doing, but for teaching somebody how to do X it’s really terrible, and for somebody who knows exactly what they need to be doing it’s mostly just standing in the way.

      2. It’s quite clear Arduino’s are not aimed at CS and EE students or working professionals who have the time to digest 3000 page data books and have a full lab of test equipment at their disposal. But instead aimed at artists and hobbyists on a budget. Remember Banzi created it to compete with the Parallax Basic stamp which was all the rage at the time. It took off because you didn’t have to spend half a decade of full time study at the feet of some indifferent professor at a university and spend several grand for test equipment.

        The fact that some don’t do the needed basics is not surprising. It’s not like there isn’t a plethora of electronics books aimed at hobbyists available to fill the gap. But it doesn’t make the Arduino the bad boy on the block.

        And no Arduino doesn’t make much money because their stuff is open designs. You go to Ebay and buy a Chinese knock-off for $3-15.00.

        That said, your complaints can be equally applied to any micro running Basic, Python or whatever flavor of the month interpretive language some kid managed to shoehorn into a micro. Because they all isolate you from the underlying hardware.

        1. >working professionals who have the time to digest 3000 page data books
          Actually EE are very busy with work, so they don’t have time to watch youtube video or hang out on social media site like your average ”artists and hobbyists”. Most of them work unpaid OT to meet real deadlines.

          They however have learnt the skills for speed reading knowing where the 10 pages of relevant info might be out of the 1000+ pages. Information is power. Also learn to think logically than wasting time blindly copy/paste things.

          If you are too lazy to develop these type of skills, you are forever trapped in your inferiority complex.

        2. Or mechanical engineers in need of some basic automation control, or systems engineers, or…

          It’s a quick way of hacking something together if you already know what you’re doing.
          Anywhere you need to actually learn something, Arduino is not the way.

        3. >But it doesn’t make the Arduino the bad boy on the block.

          Actually, it does, because it’s being marketed as the “learning aid” into microcontrollers, so students and teachers alike (!) believe it. It’s bringing down the level of education, and the students no longer bother even to read datasheets because they seem to assume they can just go on the Arduino forums and someone will have done the work for them.

          It all becomes cargo cult engineering. Someone has some incomprehensible piece of code – let’s copy paste that and hope for the best.

        4. >And no Arduino doesn’t make much money because their stuff is open designs. You go to Ebay and buy a Chinese knock-off for $3-15.00.

          And yet people buy the products because regular stores don’t sell the Chinese copies. People who already know what they’re doing may buy the knock-offs from China, but all the people I have seen around tinkering with Arduinos have bought them through official channels that sell the genuine brand.

          When the example code you copy/paste from online says it works on an Arduino Uno, the students buy an Arduino Uno, because they don’t even have the technical understanding to know that the board with a different name works just the same.

      3. Yup. Exactly the same as the PLC market.

        It would be a more valid complaint except the huge part you are skipping is that if the TEACHER knew about how servos worked it takes a few minutes to explain it, same as all the rest. But the teachers don’t, which means they won’t be able to tell them any of the lower level things you hold most dear. Electron band gap explanations and hole migration should be kindergarten subjects, right?

        1. The teacher is not going to be standing over the students shoulder to explain changing technology for the rest of the students career.

          The student needs to learn to stand in their own two feet, do the research or as we used to say … RTFM.

          If the student can’t or is unwilling to research a simple hardware protocol like servo frames then they deserve a poor mark.

      4. STEM teacher here. When I introduce my students to servos, the first thing we do is look at the pulses on an oscilloscope. We also generate pulses “by hand” using lots of delay()s. Here I introduce direct port manipulation and we look at the size of compiled code. Then we talk about why we use the built in servo() commands, and why they don’t work on e.g the ATTiny85.

        The Arduino isn’t the “problem,” it’s just one tool to teach “stem stuff.” Most of the 16-17 year olds i teach will not go on to become engineers, and if they do, there are university courses for that. Part of what we do in high school is introduce concepts, so that students can look at cool stem stuff and (hopefully) realise “hey wow i can apply that to my cosplay/custom game controller/car/bike/little brother’s birthday card” and then if they have the interest, go and learn about P and N doped silicon themselves :-)

        No course of study can teach a student _everything_, at some point we expect them to follow their own interests, and have enough sense to end up somewhere like HaD or stackoverflow instead of quora or yahoo answers :-)

        Oh and my class on resistors specifically talks about pinching resistors in your hands when measuring them(I’m sure most classes do! I am not claiming to be anything special as a teacher!) As I’m sure you know, it’s pretty insignificant for anything under about 100k.

        It’s an ongojng tradeoff in teaching (and the subject of much diacussion among teachers) about how much depth we go into vs. how many students will actually take the course. There is no “correct” answer here :-)

    6. Tools like this and Arduino get more kids into this at younger ages than ever before. If you are serious about it, you delve in deeper. As deep as you want to. There’s so many processors and devices now, code examples are king towards helping choose what goes into a product. Especially considering the crazy product schedules being pushed today by rabid marketing people. It’s all good. I started with 8080 assembly back in the day.

      1. Same here,

        But yeah code examples and good tutorials are king especially for newbies getting into embedded controllers and electronics. And nowdays there are plenty of books that teach the basics quite well, even for Arduino users who want to extend their knowledge and capability.

      2. That would be neat if the people did pick these things up when they’re 12. In reality they pick them up when they’re 22 and just weeks from completing their very first assignment in some engineering course.

        1. Pretty defeatist attitude toward a learning tool, amigo. You know what they say – once you know better, you do better.

          So you start out by buying an Arduino, at the last minute. And you hook a servo up to it, and it doesn’t work, because you don’t understand how the Arduino abstracts out crucial things that make a servo work right. At that moment, you’re at a crossroads, and one of two things will happen.

          1. You give up. I don’t know how to make this servo work, you say. Great! It wasn’t important to you! At least the Arduino was cheap! Give it to your nephew. If he solves the problem first try, perhaps by googling why this would happen, don’t be too upset. You took a bump on your ego but you helped your nephew get into hacking.

          2. You bang your head against it, get frustrated, start googling, start finding out exactly what the Arduino is doing and why it isn’t working here. You engineer a solution. And maybe it’s a yucky solution involving delays in your code to con the Arduino into paying attention or whatever. You get the servo working. Great! That’s what you wanted. Thanks, Arduino, I passed the class! And now you start to wonder… is it possible to avoid this whole issue for next time? Wow, there’s pretty easier ways to drive the servo with simpler hardware? Maybe I should get on a message board and start asking questions…

          This is the reality of how a hacker is made… is it not? How you gonna know better, and thus do better, without a gateway drug? Is it just not possible that that be the Arduino?

  1. Very Nice! I love tools this sort of tools.
    Does anyone know which library/framework allows you to make such interface? Qt?
    Or can someone recommend one?
    Sure there is Java or C#, but it seems like a I need a huge framework just to have a window with 2 buttons to push..

    1. Stockvu here…
      THank you for the kind words…
      RTM_TimerCalc was built using Visual Basic dotNet Community V15. It employs Zedgraph ( a free charting C# UserControl) to draw Timer count depictions and resulting waveforms. I have no plans to deploy it as a web app or plug-in — sorry.

    1. How is it resource hungry, it’s just using a hardware timer for the count and the functionality for top overflow and compare a and b are implemented in hardware to generate the pulse waveform (at least for the atmega328p and 32u4 that I most often use)? I could understand on much cheaper/older micros you’d have the overhead of implementing that in an isr, but for the supported chips that this software is made for that isn’t the case.

      1. I suspect previous poster doesn’t understand that this produces code that you then copy and paste into the compiler that’s producing assembly for the AVR, but thinks this program is run on the AVR itself to generate code for the AVR. Because, otherwise, yeah, it doesn’t make any sense as the AVR’s hardware PWM requires zero processing overhead once set up, and even the setup is only 3-12 processor cycles on powerup.

      2. PWM on most microcontroller are set and forget. i.e. works on its own without needing to be serviced by the microcontroller or IRQ.
        If you have to generate multiple clock edges within a period e.g. SPWM, then you would need to feed in next set of compare value with IRQ. On ARM chips, you can even program a whole table to automatically load in next set of compare value using DMA for complex waveform without bothering the CPU.

  2. This isn’t new, if memory serves. 6-7 years ago there was this thing called WebbotLib for ATmegas that had a tool like this. It was an attempt at offering the same experience you got with the Arduino environment without having to use an Arduino. It was pretty good, but I see the site is down, so it was probably abandoned.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.