PWM on Arduino

I’ve spent a lot of time recently talking about different aspects of PWM and filtering. Today I’d like to take a step back from the theory and build what you might call the “Hello World” of digital synthesis, generating PWM signals with an Arduino. This should be a fairly straightforward project but will give me a vehicle to start experimenting with pulse width modulation in a more concrete setting.

Wiring It Up

Arduino PWM Circuit

The wiring for this one is pretty simple. On one side of the Arduino I’ve placed a 10K potentiometer between 5V and ground with the wiper connected to pin A0. This is the analog input that I will use to set the duty cycle of my pulse. The potentiometer will function as a basic voltage divider allowing us to control the voltage at the pin (between 0 and 5V).

On the other side of the Arduino we find the digital pins. These are where we can output our PWM signal. You’ll notice some of the pins (3, 5, 6, 10 and 11) have a tilde beside them. These are the pins capable of outputting PWM signals. An additional consideration is that Arduino UNO boards offer two different PWM frequencies. Pins 3, 10 and 11 output PWM at 490Hz while pins 5 and 6 output 980Hz. For this project I’ll be connecting my scope to pin 5 to read a signal output at that pin.

Writing The Code

The code for this project should be pretty straight forward as well. I’m writing it using the Arduino IDE which is available free from the Arduino website. I’m going to break it into a few sections and explain each one.

int duty_cycle_in = A0;
int PWM_out = 5;

int duty_cycle;

The first step is to declare a few variables. Here we define our duty cycle control to be at pin A0 and our PWM output to be at pin 5. This step can be skipped if you refer to the pins directly when you use them in your program however as your code grows this can make things really confusing. For the sake of keeping clear and concise code I always alias my pins in this way.

Additionally I create an integer variable to store the duty cycle. This variable will be updated with a value from duty_cycle_in and be used to write the PWM signal to PWM_out.

void setup() {
  pinMode(PWM_out, OUTPUT);
  pinMode(duty_cycle_in, INPUT);
}

Next up is our setup function. This function will run once when the Arduino powers on before moving into the main loop. Here we call the pinMode function twice. This function tells the Arduino what kind of information to expect from/send to each pin. We set PWM_out (pin 5) as an output and duty_cycle_in (pin A0) as an input.

void loop() {
  duty_cycle = analogRead(duty_cycle_in)/4;
  analogWrite(PWM_out, duty_cycle);
}

Finally we come to the main loop of the program. This is where the magic happens, the main loop is the code which runs on a repeating cycle as long as the Arduino is powered on.

The first step is to read from the duty_cycle_in pin (A0) and store that value into the duty_cycle variable. Notice that I divide the result by 4, this has to do with a difference in resolution between the analog input pins and PWM output pins. the analogRead function returns a value between 0 and 1023 depending on where the voltage falls between 0V and 5V. Meanwhile the analogWrite takes a value between 0 (always off) and 255 (always on). To deal with this discrepancy we simply divide the input by 4 to bring it from 0-1023 down to 0-255.

Next we take that duty_cycle value and write it to the PWM_out pin using analogWrite. together these two lines mean the Arduino will be constantly checking the value at the potentiometer and writing that value to the PWM pin as a duty cycle (with an average voltage equal to the value read from the potentiometer).

NAND Drone

NAND Drone Synth

I’ve built a lot of square wave synthesizers. They are some of my favorite projects. They’re generally fairly straight forward but can provide hours of fun. Further they provide a great starting point for newcomers to audio electronics. That being said after stringing together so many 555 timers and 40106 inverters I was looking for a way to breath some new life into these builds. I decided to start playing with some simple digital logic to see if I could develop a deeper sound while still sticking with basic square oscillators. What I came up with was a super-simple drone synth using NAND gates to combine three 40106 oscillators.

Parts List:

2 – 40106 Hex Schmitt Inverter ICs (One will work but 2 is ideal)
1 – 4011 Quad NAND Gate IC
3 – 0.01 uf Capacitors
3 – 500K ohm Potentiometers
6 – 20K ohm resistors
3 – 10K ohm resistors

Pretty short parts list right? This one comes together pretty easy thanks to the 40106 oscillators low parts count. Even with so few parts though, it still sounds awesome. So lets have a look at the circuit.

Circuit Diagram

NAND Drone Synth Circuit Diagram

Note : I’ve split the 40106 inverters between two chips. Since the 40106 includes 6 inverters this can be completed with 1 40106 chip. However, the 40106 can act unpredictably when all 6 inverters are switching at high speed so the behavior may not be optimal.

This circuit can be split into a few simple parts which I’d like to have a look at seperately.

Square Wave Oscillators

This circuit uses 3 40106 Inverter based oscillators. I used these oscillators primarily for their relative simplicity though essentially any square source could be fairly easily implemented in this design. I used a 0.01uF capacitor to set the frequency range for the oscillation but feel free to experiment with other values to get the frequency you desire. I then limited the current using a 500K ohm potentiometer to allow me to move through a wide band of frequencies.

Buffers

After the square wave is created each signal is passed through an additional inverter. The frequency of the 40106 oscillator is defined by the charging speed of the capacitor. This capacitors charging is a directly result of the current flowing out of the output and through the resistor. This means any addition power draw we add to this output will impact the frequency. To avoid this we send the signal through the additional inverter which isolates any further components from the oscillator circuit.

Attenuation

Next up we pass each signal through a basic voltage divider. This is simply to lower the amplitude of the inverter outputs before sending them into the 4011 Quad NAND chip.

NAND Gate

Here’s where the magic happens! The signals are each connected to two of the NAND gates such that each NAND gate has a unique set of two input signals (Sounds a bit weird but should be clear if you look at the circuit diagram). These NAND gates will be high by default but will go low when both inputs are high. This means any pair of your oscillators going high at the same time will cause at least one of the NAND outputs to go low.

Mixer

Finally we send each NAND output through a 10K ohm resistor and combine them into one output line. You can then wire this to the output style of your choosing and start making noise. Note the output from the 4011 is fairly low amplitude (about 1.5V peak to peak) so you may need to amplify it before connecting to a speaker.

Bigger is Better

I chose to use three oscillators here for convenience however there is no real need to stop there. Just be aware as you grow the number of NAND gates needed to have every combination will grow very quickly. For 4 oscillators you would need 6 NAND Gates, with 5 you would need 10 and so on.

Sound Waves

I just wanted to finish off by showing some of the sound waves I captured on my oscilloscope to give you an idea of what this circuit outputs:

40106 Oscillator Continued

40106 Oscillator Schematic

I left my last post (40106 Triangle Waves) on a bit of a cliff hanger. I had shown how to pull a triangle wave from the circuit and identified some issues with the oscillator as it stood. Today I’d like to go over those issues and how I corrected them to get this oscillator up and running.

I do want to mention the oscillator design above is still in a somewhat rudimentary form. I expect there will be quite a bit of optimization that can be done on it as time goes on.

Buffering

Since we are pulling our outputs directly from the loop which sets the frequency, any elements we add to the circuit which draw current will affect the frequency. This is not ideal. What we need to do is isolate the oscillator circuitry from any further additions to the circuit. For the square wave this is extremely straight forward. Since the signal is binary (digital) we can simply send the output through a second inverter on the 40106 chip. Take note: this will invert the signal (when the original oscillation was high the buffered oscillation will be low) however since this is a repeating signal it won’t cause any impact.

The triangle is slightly more difficult to buffer since we need to concern ourselves with a range of analog values. I accomplished this by feeding the signal through an LM324 op-amp set up as a voltage follower with an additional 10uF capacitor on the output.

Amplification

After the buffering stage I was still left with dramatically different amplitudes for the two wave forms. The square wave after the buffer sat at almost 9V while the triangle was only 1.4V (The output voltage of the LM324). There’s a number of ways you can approach this inconsistency however I found myself somewhat limited by the parts I had on hand and by my decision to run this oscillator on a single 9V battery supply.

How I ended up overcoming this was by using voltage dividers to lower both signals to about 1V peak to peak. From here the selected input is sent into a very basic LM386 power amplifier.

This solution does introduce a large amount of noise into the square wave signal so you may choose to bypass the amplifier with the square wave and only use it on the triangle.

Set up this way I got both signals to reliably output approximately 5Vpp.

Decoupling

One issue I ran into a lot with this circuit, especially building on breadboards was noise. The frequency would bounce around and the wave-forms would not be crisp. This can be largely overcome by adding decoupling capacitors between the negative and positive supply lines. An excellent overview of decoupling (and many other common capacitor uses) can be found over at SparkFun.

40106 Triangle Waves

Recently I completed a post discussing the 40106 Inverter and a simple square wave oscillator. I wanted to build on that post a little more today and look at how we can modify this same oscillator to output triangle waves along with square. These triangle waves will sound profoundly different than square waves in the audible range, giving our oscillator 2 distinct voices. Additionally, we can use the oscillator at a low frequency to drive a voltage controlled amplifier or filter. We can get much more variance from the rising and falling triangle wave in these applications than we would with the simple switching of a square wave.

So Where Are These Triangles?

40106 Oscillator with Triangle Output

If you read my previous post you may remember that this oscillator works by charging and discharging a capacitor. As the capacitor charges and discharges it allows the voltage to rise and fall at the input. This rise and fall causes the output of the inverter to turn on and off producing the square wave. What we can also do is take an output at the input of the inverter where the voltage is rising and falling to produce a triangle wave!

An Amplitude Problem

If you try building this circuit as is you’ll very quickly notice an issue with the design. Connecting a speaker to the output of the square wave sounds great but the triangle is barely audible! If you look at the two waveforms above you’ll see that the square wave has a peak to peak voltage of 6.32V. The triangle however is only registering a fraction of that at 1.22V. This is because the output of the inverter (where we draw the square) always outputs a full digital signal. Meanwhile the voltage at the input only rises until it reaches a threshold voltage. At that point the inverter changes state and the capacitor begins discharging again. In this case (running the 40106 on 9V) that threshold appears to be 1.22V.

So What Now?

In my next post I’ll be exploring the use of op-amps to buffer these outputs and equalize them to a usable voltage. Once we’ve accomplished this we’ll be able to start using this oscillator in all kinds of awesome projects. See you all soon!

40106 Hex Schmitt Inverter

The 40106 Hex Schmitt Inverter is an incredibly useful and popular IC in the world of DIY synthesis. It is cheap, easy to use and is central to one of the simplest oscillators around. Today I’d like to have a look at this chip, explain how and why it works and show you how you can use it to start making some noise.

A Hex Schmitt What-Now?

As the name suggests the 40106 chip is a Hex Schmitt Inverter (Or 6 Hex Schmitt Inverters) on a 14 pin chip. An Inverter is a digital component which takes an input (0 or 1) and outputs the opposite value. Typically in digital electronics these would be represented as 0V or 5V meaning if 0V is sent to the input 5V will be output by the output and vice versa. What makes a Hex Schmitt Inverter special is its capacity to take analog inputs rather than just 0 or 5V. The way this is accomplished is by setting a threshold voltage where the output changes. Looking off the datasheet for the 40106 we can see that in typical operation this happens at 0.9V when the chip is being powered with 5V or 2.3V when being powered with 10V. When the voltage on the input goes above that threshold the output turns off. When the input goes below that threshold the output a digital high voltage (usually 5V).

Dividing By Zero?

This Isn’t Going To Work

That probably all sounds as clear as mud so lets go over a simple use case to see if we can make some sense of it. We know that when you input a high voltage to an inverter it outputs a low voltage and vice versa. So what would happen if we connected the output back to the input? Now we’ve built a bit of a paradox! When the output is high it sends that high signal back to the input which makes the output low which makes the input low which makes the output high which makes the output low which makes…. you get the idea. The problem is since this is happening instantly its faster than the chip can handle and the whole thing breaks down.

Capacitor To The Rescue!

Simple 40106 Oscillator

What we need is a way to delay the signal traveling from the output back to the input so we can get a consistent oscillation. We can accomplish this by adding a capacitor between the input and the ground and a resistor between the output and input. The capacitor is initially in its uncharged state and the input is low. This low input voltage causes the output to go high, however an uncharged capacitor provides no resistance between the input and ground so all current flowing out of the output goes to ground and the voltage stays at 0V at the input.

As current flows into the capacitor it begins to charge which in turn resists more current traveling through it. This new resistance allows the voltage on the input to begin to grow proportional to the resistance provided by the capacitor. Eventually this voltage will reach the threshold voltage of the inverter and cause the output to go low. Then the whole thing happens in reverse, as the capacitor discharges the voltage drops until it falls below the threshold voltage and the output switches back to high.

The resistor functions to limit the current traveling from the output to the capacitor which slows the charging of the capacitor.

So How Do We Control This Thing?

Simple 40106 Oscillator with Control Pot

The key to taking this from a curiosity to something useful is control, we need to be able to select a frequency range and modify it in real time. Since the speed the inverter flips from high to low and back is governed by the charging and discharging of the capacitor we can control the frequency by controlling the speed the capacitor charges and discharges.

The first way to do this (as you may have guessed) is by changing the size of the capacitor. A smaller capacitor will charge quickly providing you a very high frequency while a larger capacitor will charge slower and provide a substantially lower frequency. Choosing the right capacitor is a great way to select a range of frequencies for your oscillator however as variable capacitors are rare and expensive this is not an ideal method for making real time changes to the frequency.

This leads us to the second method which is adjusting the resistance. This resistance limits the current flowing to the capacitor. The less current flowing to the capacitor the slower it will charge. Further since potentiometers (variable resistors) are common components we can add a knob to adjust the frequency of our oscillator on the go.

An easy way to calculate the approximate frequency with any resistor capacitor combination is using the equation f = 1.5/RC

555 Based Drone Synthesizer With LFOs

Today I have an update to the 555 Based Drone Synthesizer which I posted last week. I was having a lot of fun with my Drone synth but wanted to expand on it to get a bit more variation from the sound. To do this I added a set of LFOs to modulate the frequency of the drone oscillators.

To accomplish this I built a second set of three 555 based oscillators. These new oscillators are identical to the ones in the drone synthesizer except for a change to the capacitor between pin 2 and ground. By increasing this capacitor from 0.01uf to 1uf I was able to lower the frequency of the square wave they produce. This makes them perfect for use as LFOs.

I then connected the output (pin 3) of each of these new oscillators to the control voltage input (pin 5) of the corresponding oscillator in the drone synthesizer through a 1K resistor. An additional modification that can be made to this circuit to provide further control would be to replace this 1K resistor with a 10K or 50K ohm potentiometer which would allow you to modify the amount which the LFO modulates the oscillator in the drone synth.

In this experiment I have used LFOs but you could easily control the drone synth in other ways as well by providing a control voltage to pin 5. I am interested to see how this set-up would react to a control from a sequencer or keyboard and may try this in the future.

Simple 555 Based Drone Synth

Today I have another very simple breadboard project based around the 555 timer. I’ve built three 555 astable oscillators similar to what I used in my 555 Serial Oscillator Project. This time however instead of connecting the oscillators in series I have run their square wave outputs through a simple mixer onto a single audio channel. This is known as a drone synthesizer.

As you can see from the schematic this drone synth is a fairly straightforward build. If you are familiar with 555 oscillators you probably recognize the layout of the 555 chips as a basic astable oscillator almost directly out of the 555’s datasheet. Once the three oscillators were created I added a 1K ohm resistor to the output of each (pin 3) and connected these outputs together. Once the three outputs are connected you can run this through a large capacitor (I used 330 uf) and out through your output jack. It is also possible to connect this circuit directly to a speaker though the audio level may be a bit low without amplification.

Once you’ve built this circuit you can begin experimenting with it. Pin 5 on the 555 chip can be used as a control voltage input so you could easily add a method of control voltage to this circuit. The easiest way to do this would be to build three more 555 oscillators and connect their outputs to pin 5 of the 555 chips in your drone synth. These will function as basic LFOs and modulate the frequency of the drone’s oscillators. You could also experiment with other voltage controls like a sequencer or keyboard. You could also replace one or more of the potentiometers with things like photo-resistors or body contacts to make the project even more interesting.

Lastly I wanted to mention that this drone synth is not limited to three oscillators. You can add as many 555 oscillators as you want in parallel to make an even deeper and stranger sound. If you’re interested in seeing the extreme of this a YouTube user by the name of Look Mum No Computer recently built a 100 Oscillator Drone that is definitely worth checking out.

Atari Punk Console – Adding LFOs

Today I wanted to show you guys something new I’ve been playing with on the breadboard. Essentially what I have here is a twist on a classic project. I’ve taken an Atari Punk Console (Stepped Tone Generator) and added two 555 timer based oscillators as LFOs to add more depth and interest to the sounds produced.

This modification is built on one simple characteristic of the 555 timer (and by extension the 556 which consists of two 555 timers). This is the control voltage pin which allows you to use an external voltage source to modify the chips performance. On a traditional 555 timer the control voltage input is located on pin 5 while on the 556 you would use pins 3 and 11 as I have here. By connecting the outputs of two 555 oscillators to these pins on the 556 we are able to use them to modify the frequency of the stepped tone generator.

You may notice in the video I play mostly with the left LFO knob. This is because in my experimentation I was able to illicit much more noticeable effects from the first oscillator connected to pin 3 of the Atari Punk Console. The second oscillator (connected to pin 11) only seemed to have a pronounced effect at fairly high frequencies. For this reason you may want to try switching the 500K pot on the second oscillator for something smaller like a 100K or 50K. Another option to increase the frequency range is to change out the 3.3uf capacitor for a 0.1uf cap.

Another thing you can do with this circuit to gain further control is to add a potentiometer to control the modulation of the LFOs on the Atari Punk Console. To accomplish this you can add a potentiometer in place of the 10K resistor at the output of the LFO oscillators. A 50K pot would likely work best for this.

Finally if you enjoyed this project I did build an Atari Punk Console back in the early days of this site with jacks for control voltage inputs. Feel free to have a look if you are interested.

 

555 Based Piezo Trigger

I’ve always been drawn to drum pads and kits. They are lots of fun and offer a slightly more tactile method of control then rows of pots and switches. So today while I was playing around on my breadboard I was drawn to pull out some piezoelectric disks and start experimenting.  What I’ve come up with is a very simple drum trigger circuit that you can build and experiment with.

This circuit uses a 555 timer set up in monostable mode. A monostable 555 timer will output a square wave pulse whenever it receives a trigger pulse from the piezo disc at pin 2. The pulse output from the 555 can then be adjusted through the 500K ohm pot placed between V+ and pin 7. The output pulse is then sent into the base of a 2N3904 transistor which works as a gate between the audio source and the speaker. This means when the pulse from the 555 is high the audio will pass through the transistor and when the pulse ends and the 555 output goes low the transistor will block the audio from passing.

If you are interested in adjusting the pulse length beyond what is available using the pot this can be achieved by adjusting the electrolytic capacitor between pin 6 and ground. By lowering the value of this cap you can shorten the range of pulse lengths available. Conversely by increasing it you can access a longer range of pulses.

By setting up 4 or 5 of these piezo trigger circuits you could create a fairly versatile set of drum pads. Since the audio source can be switched out or developed further there’s a lot that you can do to expand on the acoustic possibilities of your drum kit. You can try experimenting with different oscillators, Filters, LFOs, White Noise Generators or anything you want.

555 Oscillators in Series

I’ve been spending a lot of time lately playing around with 555 timer chips and wanted to quickly share my latest creation. This project uses four 555 timers each set up as a standard astable oscillator. I’ve then connected the output from pin 3 of each oscillator to the control voltage at pin 5 of the next subsequent chip. Essentially this means each 555 timer is working as an LFO for the next oscillator to it’s right. I also increased the size of the capacitor between pin 6 and ground of the two left-most oscillators in order to  lower their frequency.

I was quite pleased with the range and depth of sounds it produced however, it should be said that I built this as a proof of concept and it is not fully flushed out. I would be very interested to try a similar setup with a different waveform. I feel like this idea would really come into it’s own if used with a triangle or sine wave oscillator which produced a wider range of tones. I have also been experimenting, with some success, with adding capacitors between the output and control voltage inputs to smooth the square wave slightly and create a saw tooth pattern. Without an oscilloscope on hand however this is proving difficult to optimize.

This is also a circuit which can be easily expanded by adding additional oscillators and admittedly there is a little voice screaming in my head to take it to it’s logical conclusion. I expect in my near future I’ll spend a rainy afternoon stringing together as many 555 circuits as I can fit on my breadboards and see what I end up with. I’ll be sure to share the results.