Posts | Comments

Planet Arduino

Archive for the ‘PWM’ Category

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.

In the midst of striking for climate change awareness, you may need some extra hands. That’s what [Anred Zynch] thought when they built Strettexter, the text-spraying writing robot that sprays onto streets.

The machine is loaded with 8 spray cans placed into a wooden box (a stop line with a wooden ledge to prevent the cans from falling out) and is fixed on top of a skateboard. It uses a PWN/Servo shield soldered onto an Arduino Uno connected to 8 servo motors (TowerPro SG90s) to control each of the spray bottles. A table converts every character into 5×8 bit fonts to fit the size of the spraying module. The device also includes a safety switch, as well as an encoder for measuring the horizontal distance traveled.

The Strettexter is activated by pulling on the skateboard once it’s been set up and connected to power (for portability, it uses a 8000mAh power bank). In its current configuration, the words stretch out pretty long, but some additional testing will probably lead to better results depending on the constraints of your canvas. The shorter the words, the more difficult it is for the white text to be legible, since there is significant spacing between printed bits.

We don’t condone public vandalism, so use this hack at your own discretion.

[Thanks neiß for the tip!]

As if you already weren’t agonizing over whether or not you should build your own arcade cabinet, add this one to the list of compelling reasons why you should dedicate an unreasonable amount of physical space to playing games you’ve probably already got emulated on your phone. [Rodrigo] writes in to show off his project to add some flair to the lighted buttons on his arcade controller. (Google Translate)

The wiring for this project is about as easy as you’d expect: the buttons connect to the digital inputs on the Arduino, and the LEDs on the digital outputs. When the Arduino code sees the button getting pressed, it brings the corresponding LED pin high and starts a fade out timer using the SoftPWM library by [Brett Hagman].

It’s worth noting that the actual USB interface is being done with a stand-alone controller, so the Arduino here is being used purely to drive the lighting effects. The more critical reader might argue that you could do both with a single microcontroller, but [Rodrigo] was in a classic “Use what you’ve got” situation, and already had a USB controller on hand.

Of course, fancy lit arcade buttons won’t do you much good without something to put them in. Luckily we’ve covered some fantastic looking arcade cabinets to get you inspired.

If your Arduino runs out of I/O lines, you can always add one of the several I/O expander chips that takes a serial interface to set its several pins. Or perhaps you could buy something like an Arduino Mega, with its extra sockets to fulfil your needs. But what would you do if you really needed more pins, say a thousand of them? Perhaps [Brian Lough] has the answer. OK, full disclosure: If you really need a thousand, the video isn’t exactly for you, as he shows you how to add up to 992 PWM outputs. The chip he uses works with any microcontroller (the video shows an ESP8266), and we suppose you could use two daisy chains of them and break the 1,000 barrier handily.

We like how short the video is (just two minutes; see below) as it gets right to the point. The PCA9685 chip gives you 16 12-bit PWM channels via an I2C interface. You can daisy chain up to 62 of the boards to get the 992 outputs promised.

[Brian] uses a cheap $2 breakout board that lets you set a 6-bit address, has a nice power connector and makes it easy to use the little surface mount device. Each of the 16 outputs on the board can have an independent duty cycle, but they do share a single output frequency. That means if you want to use some channels for low-frequency devices like motors and some for high-frequency devices like LEDs, you might have to spring $4 for two boards.

Over on Hackaday.io, we’ve seen these devices driving 128 vibration motors. The PCA9685 made us think of the time we rolled our own serial to PWM devices using an FPGA.

How many times have you wished for a pocket-sized multimeter? How about a mini microcontroller-based testing rig? Have you ever dared to dream of a device that does both?

Multiduino turns an Arduino Nano into a Swiss Army knife of portable hacking. It can function as an analog multimeter to measure resistance, voltage drop, and continuity. It can also produce PWM signals, read from sensors, do basic calculator functions, and display the health of its rechargeable battery pack.

Stick a 10kΩ pot in the left-side header and you can play a space shooter game, or make line drawings by twisting the knob like an Etch-A-Sketch. Be sure to check out the detailed walk-through after the break, and a bonus video that shows off Multiduino’s newest functions including temperature sensing, a monophonic music player for sweet chiptunes, and a virtual keyboard for scrolling text on the OLED screen. [Danko] has a few of these for sale in his eBay store. They come assembled, and he ships worldwide. The code for every existing function is available on his site.

More of a maximalist? Then check out this Micro-ATX Arduino.

Thanks for the tip, [Rahul].

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);
  }
else
{ ...

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:


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 Hackaday.io page, as well as find documentation and ready-to-use example sketches GitHub.



  • 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