DAC conversion

A "new fashioned" televisor, using an Arduino to drive the motor and display.

Moderators: Dave Moll, Andrew Davie, Steve Anderson

DAC conversion

Postby Klaas Robers » Sun Mar 19, 2017 12:57 am

Why not making an D to A convertor? Just simple resistors in values 1, 2, 4, 8, 16....... ? Or don't you have sufficient output pins for it? The advantage is too that you may do gamma in the analogue domain, two diodes in the emitter chain of the transistor. Then the D to A may be 6 bits , 64 steps....
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: WAV file handling

Postby Andrew Davie » Sun Mar 19, 2017 1:10 am

Klaas Robers wrote:Why not making an D to A convertor? Just simple resistors in values 1, 2, 4, 8, 16....... ? Or don't you have sufficient output pins for it? The advantage is too that you may do gamma in the analogue domain, two diodes in the emitter chain of the transistor. Then the D to A may be 6 bits , 64 steps....


Because I never thought of the possibility. I think I have sufficient digital output pins. I didn't know that you could do D2A via this way. So I think what you're saying is that on each of the 5V outputs (I have 7 of them, say)... I have a resistor. Each pin is a binary "bit" in a 7-bit analog result. The resistor modifies the current from each pin, and they're in powers of 2 to represent the bit value of each pin. The total resistance is in series (?) so we add them all up to give a total resistance and hence a total current across the whole assembly. That current is the D2A conversion of the on/off state of the 7 pins, giving us 128 levels of current or basically 128 pixel shades.

I'm reasonably sure I have enough pins, possibly even for 8 bits.

Edit: Note to self: see Resistor Ladders
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: WAV file handling

Postby gary » Sun Mar 19, 2017 11:08 am

It's a tried and proven approach Andrew, are you an NBTVA member? There are examples in the Newsletters.

The resistor ladder method is what I used in the MBTV demonstration I posted here a few days ago, it is only a 2 bit version but the principle is scalable to any bit depth limited only by how many output pins you have available.

BUT! You are effectively doing the same thing with the MCP4151 except it has the "apparent" advantage of having a serial input (there are serial input DACS as well BTW). I seriously doubt the MCP4151 approach will work but I do think it is worth a try as it would be quite an elegant solution.
gary
 

Re: WAV file handling

Postby Klaas Robers » Sun Mar 19, 2017 10:18 pm

Andrew, the trick is to connect resistors from the output pins of the Arduino to a central point, say the base of a transistor. The MSbit has a resistor of 1k, the next 2k, then 4k, 8k, 16k, 32k, 64k for the least significant bit.

If we assume the voltage on the base of the transistor to be fixed, say 0 volt, then a 1 on the most significant bit (5 volt) gives a current of 5 mA to the base, the next 2.5 mA, then 1.25 mA and so on. So the base input current is an analogue representation of the digital outputted number.

Good to know is that it is not needed that the voltage at the base has to be 0 volt. It is even possible to have the base point floating. Then you get a voltage from 0 tot 5 volt analogue to the digital outputted number. So a resistor from the emitter to ground gives an analogue current from the collector to the LED cluster. With two diodes and 3 resistors, well choosen of course, you perform gamma correction. You can find this done in the RGB drivers following Karen Ortons PIC NBTV colour decoder.
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: WAV file handling

Postby Klaas Robers » Sun Mar 19, 2017 10:36 pm

There are certain rules for the precision of the resistors. The resistor for the least significant bit may have a precision of say 30%, as that gives just one step in grey, so if that is somewhat, half a step, more or less, who cares.
The precision goes up by a factor of two each next bit. For 7 bits this goes:
0 : 64k -32%
1 : 32k -16%
2 : 16k -8%
3 : 8k - 4%
4 : 4k - 2%
5 : 2k - 1%
6 : 1k - ½%
This is the reason that DACs are in most cases integrated. It is difficult to make very precise resistors, but in an IC you can easiliy make resistors that have the same value. By circuiting them parallel or in series you may make a precise DAC. Karen Orton has chosen for discrete resistors, as that is easier understandable.
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: WAV file handling

Postby Andrew Davie » Sun Mar 19, 2017 11:27 pm

Klaas Robers wrote:There are certain rules for the precision of the resistors. The resistor for the least significant bit may have a precision of say 30%, as that gives just one step in grey, so if that is somewhat, half a step, more or less, who cares.
The precision goes up by a factor of two each next bit. For 7 bits this goes:
0 : 64k -32%
1 : 32k -16%
2 : 16k -8%
3 : 8k - 4%
4 : 4k - 2%
5 : 2k - 1%
6 : 1k - ½%
This is the reason that DACs are in most cases integrated. It is difficult to make very precise resistors, but in an IC you can easiliy make resistors that have the same value. By circuiting them parallel or in series you may make a precise DAC. Karen Orton has chosen for discrete resistors, as that is easier understandable.


Thanks Klaas for your comments on this subject.
I do understand this method and the pitfalls - discontinuity for example when a higher-order bit switches on, because of the need to be very precise with the values. I will give the resistor ladder a go, just to have that experience under my belt and understand it.

Now I know a bit more about what's on the table, I understand there are pros and cons to each approach.

The resistor ladder requires log2(n) pins to achieve a given number of discrete shades. For 64, that's 6 pins. The more pins you need, the slower the main loop is, because setting up pins while not exactly slow, does require a bit of bit manipulation for multiple pinouts. Also, since there's a delay beetween processing the highest order bit and the lowest order, there are going to be intermediate analog values which don't reflect either the earlier value or the new value - but a "noisy" transition between the two, as you modify pins on/off/unchanged.

The digital potentiometer has to communicate the 0-255 value over SPI interface. This isn't slow, but it's probably not as quick as the above. It does have the clear advantage of having an instantaneous transition from one state to the other. No noise.

The PWM is extremely quick, but suffers from visible artefacts due to the rotation of the disc. Unless the PWM frequency is high enough to "pack" many cycles of PWM into a very small visible area as the disc rotates, then the eye sees artefacts. And in any case, the merging of duty-cycle on/off phases on the LEDs caused by PWM rely on the LED not moving. A rotating disc complicates things because it's analogous to moving the LED. And so the eye sees black/white instead of blended greys. You probably have to get below the angular resolution of the eye to avoid this, and this system creates a dependency between PWM frequency and disc rotation speed. If you go for a higher frame rate, you also have to go faster PWM. There are clear limits on PWM speed which make this probably an ineffective system.

Another option is a DAC chip, connected via I2C. Unfortunately there is a clash between pin 3 required by timer 0 PWM and the comms pin required for I2C. So this seems to rule out use of timer 0 which might make things difficult as there are only 4 timers available on the Arduino Micro and I'm not so sure about the 4th's capabilities.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: WAV file handling

Postby Klaas Robers » Mon Mar 20, 2017 12:18 am

It should be possible to output a digital number to a 8 pin output port in one instruction, isn't it? That is faster than what you can think of. The Arduino should have parallel digital output ports I suppose. You can connect the resistor network directly to it. In the PIC colour decoder it is also done in that way. And indeed you need no more than 6 bits / pins for 64 steps of grey.
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: WAV file handling

Postby Andrew Davie » Mon Mar 20, 2017 12:29 am

Klaas Robers wrote:It should be possible to output a digital number to a 8 pin output port in one instruction, isn't it? That is faster than what you can think of. The Arduino should have parallel digital output ports I suppose. You can connect the resistor network directly to it. In the PIC colour decoder it is also done in that way. And indeed you need no more than 6 bits / pins for 64 steps of grey.


Just looked it up. Indeed it is. I didn't know that was possible :) How neat. As it advises not to use pins 0 and 1 unless absolutely necessary (used for other stuff), then 6 bits fits well with that. So I hook up a resistor network to pins 2-7 and then I have an analog voltage from digital input pretty much instantaneously with a single register write. And the next step... that voltage resultant DAC current (because it's a resistor network changing the current based on constant voltage input) ends up controlling the current to the LED matrix... through a transistor, right?

Learning a lot!
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: DAC conversion

Postby Andrew Davie » Mon Mar 20, 2017 12:49 am

You know, it occurs to me that I'm making a lot of mistakes and "embarrassing" myself with dumb questions and exposing my ignorance - but if I wasn't presenting all of my thinking in this diary format, there's no way I'd be learning all this stuff. It's a fantastic way to learn, and I really really appreciate the advice and assistance I'm getting. Thanks Klaas and Gary in particular! I hope to stick with it, and create a beautiful, elegant, Arduino televisor to make you proud!
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: DAC conversion

Postby Klaas Robers » Mon Mar 20, 2017 5:01 am

If you just make the resistor combiner to a kind of "spider point", so a floating point, you get a voltage from 0 to 5 volt proportional to the number you write to the port. Connect your little scope to that point and write a simple program that outputs the numbers 0 to 63 to this port, not too fast, and you will see a saw tooth voltage of 64 steps.

It is easier to understand how it works if you assume the voltage at the spider point to be zero and look there at the current flowing to ground, but this works too. It is more difficult to understand, but because there is no nonlinearity in the circuit it has to work. The Arduino pulls the outputs to +5 or to 0 volt, so also the outputs are linear. May be the resistors should not be too low ohmic, Karen used resistors of
11k *
22k *
43k *
91k
180k
360k
750k
* Only the first 3 resistors need to be 1%.
User avatar
Klaas Robers
"Gomez!", "Oh Morticia."
 
Posts: 1656
Joined: Wed Jan 24, 2007 8:42 pm
Location: Valkenswaard, the Netherlands

Re: DAC conversion

Postby Andrew Davie » Mon Mar 20, 2017 6:18 pm

So I built myself a 7-bit DAC today.

dac.jpg
dac.jpg (200.99 KiB) Viewed 13090 times


This isn't just any DAC; it's a super-dooper-accurate DAC. Each of the 7 pins at bottom connects to a parallel-pair of resistors then connected in series to another, then to the pin at top-right. I spent a fair while calculating appropriate resistor values and trying values around those calculated ranges to get this very precise, even though I know I didn't really need to be this precise. But I felt like the challenge, and it was fun.

For example, here are the first 5 bits, and the reading on my multimeter (I kept all shown digits)...
(...) indicates resistors in parallel and + is the serial resistor...

(5k6 1k2) + 18 = 1.000 K ohm $1.00
(22k 2k2) + 19 = 2.000 K
(27K 4K7) + 11 = 4.00 K
(39K 10K) + 39 = 8.00 K
(39K 27K) + 10 = 16.00 K

I didn't write down the other bits, but when I do the 8th bit, I'll complete the table properly.
You can see it's as precise as my multimeter will let me measure :)

Anyway, next test is to figure out how that digital-write a byte to a set of pins works, and test out with some actual values.
I'll probably record/plot a range of values and see how accurate the resultant voltage is. I expect it to be pretty good. :P

And to those wondering why I'm spending so much time with the digital potentiometer, PWM, and now the DAC - well it's because basically I don't have much of a clue as to what I should be doing and pretty much making this up as I go. I like this resistor-DAC because it should be incredibly quick to write to the digital pins with the Arduino (one write), and should give good-enough results on the DAC. And, because I totally understand this so far.

Once I have an analog voltage and analogous current, then I can get back to driving a LED array with the current.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: DAC conversion

Postby Steve Anderson » Mon Mar 20, 2017 6:43 pm

Andrew Davie wrote:You can see it's as precise as my multimeter will let me measure.

If you (or it) changed ranges during measurement then the ranges are unlikely to match. One range (if you're lucky) maybe +1% the next -1%. And that's on a reasonably new Fluke. The resistors are 1% as far as I can see anyway.

Steve A.
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5360
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Re: DAC conversion

Postby Andrew Davie » Mon Mar 20, 2017 10:38 pm

Well my DAC works. I confirmed the input voltage at the 6 pins from the Arduino were as expected (either a very few millivolts, or close to 5V - and that checked out OK). I measured the voltage at the output pin of my DAC, and here's the plot of the results. It's pretty linear! A small kink at "32" but I'm pretty pleased with this result!

dac.jpg
dac.jpg (135.22 KiB) Viewed 13081 times


Steve, my resistor values weren't too far off - but I do understand your point about range on the multimeter.
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: DAC conversion

Postby Andrew Davie » Mon Mar 20, 2017 11:59 pm

Now I try to understand the next step.

If I consider when all 6 pins are high then that's maximum brightness, and the DAC outputs very close to 5V.

My new LED matrix will be 5 strings of 3 LEDs - so 5x25 = 125mA requirement.
The TIP31 transistor is 25x so we need 5mA at the base for maximum brightness (when V is about 5)

So, V=5 (at the DAC output, but do we cater for the drop (0.6V) across the TIP31? In that case, V=IR, so 5-0.6 = 0.005.R therefore R = 880 ohms.
So should I place about 880 ohm resistor after the DAC before the base of the TIP31 to limit current to 5mA ... ah bugger it, I just don't understand this :(

If the DAC is outputting 5V what's the current?
I'd need to know the resistance which is now a sum of resistances based on the active bits.

D0 = 1K, D1 = 2K, D2 = 4K, etc.
So at 5V, R= sum(1+2+4+8+16+32) = 63K

and I = V/R = 5/63000 = very small

On the other hand, if the pins are all 0, then measured V = millivolts - very small.
And R = pretty much 0 too - no active pins .
So current must be 0 too, right? No voltage, no current.

Let's say we have the lowest bit set - so V=5 (on the pin) and R = 1K, so I = 5/1000 = 5mA
Next pin up, V = 5, R = 2K, I = 2.5mA
How about both of those set, V = 5, R = (1+2)K, so I = 1.6mA
At the top brightness, current is very small.

This looks back to front to me - it looks like increasing brightness (increasing V) is decreasing the current.
Which, of course, you'd expect to see to obey V=IR - that is, as V increases and R increases then I should decrease.

But that's not what I want - I want the current to increase as I write higher brightnesses to the output pins.

I'm just not getting it. :(
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Re: DAC conversion

Postby Andrew Davie » Tue Mar 21, 2017 12:22 am

I don't understand. Well, I can understand if all input pins are 0V, then the result is going to be 0V. Fine.
But why is the result 5V if all input pins are 5V. Each of those pins has a resistance between it and the output. The sum of resistances for all pins is 63K ohms. Yet we see 5V at the output. The only way that can happen is if V=IR -- 5V = I * 63K. So I has gone DOWN. In fact it's pretty small (0.07mA). So the display would be darkness. That's not what I would expect when writing "63" which to me means high brightness. Is this an explanation for the "negative video" I saw in my experiments with the MCP4151 - in fact as voltage goes up on the DAC output, then current goes down - and so I need to negate the input. That seems like a rational explanation, backed up by the calculations I just did.

In which case, the V and I value (and the R, come to think of it) are set now for the base of the TIP31. The maximum is 5mA, which just happens to coincide nicely with my 125mA required for the new LED matrix, and the 25x of the TIP31. So, no further resistors required...?????
User avatar
Andrew Davie
"Gomez!", "Oh Morticia."
 
Posts: 1590
Joined: Wed Jan 24, 2007 4:42 pm
Location: Queensland, Australia

Next

Return to Andrew Davie's Arduino Televisor

Who is online

Users browsing this forum: No registered users and 1 guest