Arduino While Making a 12 bit DAC Using an Arduino – The Learning Circuit
They need to be converted again for this. We use a digital to analog converter or dac lets, take a look at how to build one and see how it works. Music. In my previous video, the lesson about dax, i explained that the more efficient version uses what is called an r2r ladder. What that means is that it uses a lot of resistors of the same value. Two of those resistors in series in the cases of two are structured in a way that breaks multiple inputs into a binary sequence. Given the layout of the resistors, each input is weighted for a binary digit, each representing one bit the more inputs, the more bits, the more bits, the higher the resolution, the more accurate the conversion from digital to analog. With the r2r structure, each input connects to two resistors, giving the value 2r, which in turn connects to a series of r value resistors with each input connection. Dividing that series for our test circuit. Each input will be one of the arduino digital inputs, the beginning of the resistor series gets connected to ground, while the end of the resistor series allows us to view the output wave for this demo. I need an arduino uno, a usb cable for programming, a breadboard, a bunch of jumper wires and a whole lot of resistors of the same value on my breadboard. One resistor will straddle the rows with another connecting and running perpendicular right now, thats rr so ill.
Add a second resistor connecting in the top set of rows to give us a value of 2r and that resistor will connect to one digital arduino input to add another bit. I repeat this resistor placement. The first junction of these new resistors will also connect to the loose lead of that first perpendicular resistor. Okay, that gives us a two bit digital to analog converter thats, not very useful, so lets keep going and add more bits im using the digital i o on the arduino. While there are 14 of these pins im only using 12., so thats. How many bits worth of resistors, i need to add: okay, ill, add two resistors in series to the beginning and end of the ladder, as shown in the schematic that connects the beginning to ground and the end of the series to analog input a0 on the arduino Thats it for the hardware lets take a peek at the code. We need to make our dac work and see the output. I got most of this code from a project in the arduino.cc project, hub so credit to the original authors. There are some minor tweaks and additions to the original code, for example, ill be showing different resolutions by changing the number of inputs we use. So i added these constants to help keep track of the maximum number of bits possible. Next, we create a variable that can be used to create the output sine wave and the baud rate is set to 9 600 per usual.
I had to look up how to use this next bit of code, so ill. Do my best to explain it. Lets look at the port manipulation reference page on the arduino site, port registers allow for lower level and faster manipulation of the i o pins. With three ports b, c and d. I want to use pins 0 through 11, so i need ports b and d scrolling down. I want a data direction register for ports, d and b scrolling down further. We can see how to set each pin to an input or output using binary numbers back in our arduino code, we have ddrd equals b for binary, followed by 8 bits. The least significant bit is pin 0 and up through pin 7 each marked one to set it as an output, then ddrb with 6 bits for the remaining 6 pins. We want to use pins 8 through 11, so pins, 12 and 13 are marked 0, making them inputs thats it for setup. Lets. Take a look at the program loop. We start with a for loop that generates a sine wave. The values are broken up by the bit resolution, which we set up earlier now when we go to look at the sine wave this generates, the steps may not be perfectly even due to the one percent variance of the resistors. The value of each 1k resistor can be plus or minus 10 ohms. If they were exactly the same, the steps would be identical.
Port d can only manage eight bits so assign the first eight bits as low bits. Then take the next four bits from port b and assign them as high bits. Then shift over the high bits by 8 to put them in their proper place. For a complete 12 bit number and as usual we add a short delay to help keep the code running smoothly. We include serial print dialog, so we can see the output without an oscilloscope. Now i actually want to start at a low resolution and work our way up to show you the differences, so lets bump this down to four bits. Okay, send that code to the arduino and i need to move my jumper to the fourth bit place for the resistors and ill open, the serial plotter to see our 4 bit sine wave. With this very low resolution of only 16. You can clearly see the steps, but its not very signy. Is it lets bump it up to 6 bit a resolution of 64. change. My code push it to the arduino, adjust my jumper to 6 and lets look at the serial plotter. Now the wave is much smoother than the 4 bit, but the steps are still very noticeable. Lets bump it up to 8 bit a resolution of 256. change. My resolution to 8 send it move. My jumper lets look at that plotter again now. This wave looks much more like a true sine wave, while the line is still a bit jagged.
The smooth shape is clear. This resolution could be sufficient in a lot of applications that have slow changes. Last lets bump this up to our full resolution. 496 at 12. Bit change my code, send it the arduino move, my jumper full 12 bits lets see what that plata looks like oh, its, so smooth and shiny. Look at that here. The steps are nearly imperceptible. Now, unfortunately, the serial player doesnt have a pause or a stop function. For us to take a closer look, however, i can take a screenshot and zoom in here. If we zoom in on the wave, you can see that there are still tiny steps. We can compare a screenshot of each example to clearly see the difference. Resolution makes most analog output waves are created from input waves that were originally analog, but first converted to digital. Remember that, no matter how high the resolution that original analog signal has been compressed somewhat into digital and can never be returned to truly analog again, theres always going to be some steps in that output signal. Well, i hope you enjoyed my deck digital to analog converter demonstration. I encourage you to try it at home. One thing i often do is hook up my output sine wave to lights or a speaker to get a more tangible feel for what that wave will be. Like in the physical world this time, i want you to do that so hook up your circuits.
Try different resolutions. Try different frequencies report back with your findings about how using different bits affects the light and sound output post. Your results on the element14 community on element14.com forward slash the learning circuit.