Power source affecting crystal speed?

Hopefully this is the right place to post this...

Anyway, I'm running into a weird problem that I'm hoping someone here can help me understand.

I am building a simple voltage quantizer for use with synthesizers, so basically a variable voltage enters and some processing is done and the output voltage will be quantized to some sort of musically useful values (octaves/steps/etc...)

I am using a timer in fast PWM mode and varying the duty width to create the output voltage, I am then running this through a 10k resistor and a 100n cap set up as a simple rc filter. It works very well and in my experiments I am able to create stable output voltages between 0 and 4volts. So far so good.

The next step in this project was to port the code over to an attiny. I made sure to set everything up exactly the same as it was on the arduino, using timer0 for both, using a 16mhz crystal on both etc... same exact code. However, the scaling of the output voltage was different between the two.
After doing more experimenting I realized that the scaling would change depending on the power supply I am using, on both the arduino and the attiny. USB power vs external adaptor vs attinyprogrammer usb power vs regulated 5v supply all give different scaling to the output voltage. (which kind of makes this useless...)

It seems unlikely that the clock speed would actually be changing based on the power supply, but I can't figure out whats going on here...

Any help would be most appreciated.

The next step in this project was to port the code over to an attiny.

Why? Is something wrong with the m328?

...all give different scaling to the output voltage.

You do not get zero to four volts on the output?

...using a 16Mhz crystal on both...

With capacitors?

Where's the schematic?

If you are using the default analog reference, the analog reference will be the supply voltage. So if the supply voltage changes you will get different ADC output values for the same input voltage. The answer is to use the internal 1.1V reference or a attach an external voltage reference and use anaolgReference(EXTERNAL).

Nothing wrong with the 328, but am planning on making a pcb eventually and it only really needs 2 inputs and one output, so it seems like a good opporitunity to get more comfortable with the attiny.

I do get 0 - 4v on the output. However, the output should be some some ratio to the input- like 1:1, so in practice a 1 volt input should produce a 1v output. What is happening is that depending on the power supply, a 1volt input will produce an output of 1.1 volts or .98 volts etc... so each different power supply causes the output scaling to be different.

Capcitors, yes 22pf on the crystal.

I can post a schematic if it would help.

This is the General Electronics section of the forum. You suspect a hardware problem. A schematic certainly seems appropriate. :wink:

Could always read the output back in and adjust the PWM output to make it higher lower.
Use the internal 1.1V reference so you have something stable(r) for a reference.

pre55ure:
...a 1volt input will produce an output of 1.1 volts or .98 volts etc... so each different power supply causes the output scaling to be different.

0.98 vs 1.0 is within the limits of the RC filter, the analog-to-digital converter, and the 8 bit timer. You are only going to get better results with hardware changes.

The 1.1 vs 1.0 is a different matter.

They are many things that effect the output voltage. You need to carefully separate what is significant from what is not. For example, the absolute value of the supply voltage may not be the problem. It could be electrical noise from the source.

@groundfungus and @CrossRoads have both asks questions about the reference voltage. What are you using for the reference voltage? Is the input voltage relative to the reference voltage?

Thanks for all the help.

I made a quick schematic of the circuit but couldn't figure out how to upload it here. I'll look into that later.

As far as reference voltage goes, I'm just using the supply voltage as the reference voltage. This is certainly something that I should look into as I never really though about how this might affect what is going on.

That being said, the issue isn't really on the input side (though that might be another issue I have to deal with). The incoming voltage simply selects a value from an array that controls the duty cycle of the timer output. Just to confirm that it wasn't the input voltage, I replaced the array with a static value- so that the timer counts from 0-255 and the pin is high from 0 to 63. This should produce the same output voltage regardless of the voltage reference and regardless of some noise on the input signal. Correct?

If not then I am missing something else in my understanding of this.

Here is the (simplified) code, if it helps.

//Simple quantizer code
//setup for attiny



int ocrPoolB[49] = {  0,  3,  8,  12,  16,  20, 25,  29, 33,  37,  42, 46,   50,   54,  58,  62, 67, 72, 77, 81,  85,  89, 94, 98,    102,   105, 110,  114,  118, 123, 127, 131, 135, 140, 144, 148,   152,    36, 18, 12, 9, 40, 6, 42, 10, 14, 22, 46,     203};	
int quantized = 0;


//scale07 Chromatic 
byte chromaTable[97] = { 
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
    12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 
    24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 
    36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 
    48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
    60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
    72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
    84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
    96
}; 



void setup() //setup for timer 0
{ 
  
  //attiny pins
  pinMode(2, INPUT); //input for pot to control scale select
  pinMode(0, OUTPUT); 
  pinMode(1, OUTPUT); //this is for analog signal output  
  
  
 /* 
  //uno pins
  pinMode(4, INPUT); //input for pot to control scale select
  pinMode(3, OUTPUT); //use this output for analog signal (the one we actually use)
  pinMode(11, OUTPUT); //this is for timer output
  
  pinMode(5, OUTPUT); //use this output for analog signal (this is the actual output for timer 0 on uno)
  pinMode(6, OUTPUT); //this is for timer output
  */
  
  //set up the timer
  TCCR0A = 0;
  TCCR0B = 0;  
  TCCR0A = _BV(COM0A0) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);                                      
  TCCR0B = _BV(WGM02) | _BV(CS00);    
  OCR0A = 4;
  OCR0B = 0; 
  
} //end setup



void loop()
{
  unsigned int cvIn = analogRead(A1);  //A0 for uno

  cvIn = ((cvIn+8)/17);  //determines incoming voltage note

  OCR0A = 255;                 //timer clock speed
  OCR0B = ocrPoolB[quantized]; //timer duty cycle

  quantized = chromaTable[cvIn];        
   
} //end of loop

Anyway, I really appreciate the help.

pre55ure:
The next step in this project was to port the code over to an attiny.

Which one?

pre55ure:
I made a quick schematic of the circuit but couldn't figure out how to upload it here.

• Click Reply
• Click Attachments and other options
• Click Browse
• Select the file
• Click OK / Open
• Click Post

Ok heres the schematic.
Also it's an attiny85.

Just sort of thinking out loud here... is it possible that a different supply voltage would affect the voltage at the timer pin which would then cause the capacitor to charge/discharge at slightly different rates and therefore lead to a slightly different average voltage?

is it possible that a different supply voltage would affect the voltage at the timer pin

Perhaps I've overlooked something in this discussion, but it is quite certain that this is the case. After filtering, PWM produces a defined fraction of the maximum output voltage of the pin, which is close, but not identical to the power supply voltage. If you want several devices to provide a well defined output voltage for a given input voltage, they should be powered by precision voltage regulators (of the same voltage, obviously).

The internal ADC "1.1 V" reference is stable, but is not a precision voltage source.

An alternative is to use an external DAC with a built in precision reference.

Finally, the TL072 is a poor choice for this circuit, since it is not a rail-to-rail op amp (on either the inputs or the output) intended for low voltage operation.

jremington:
Perhaps I've overlooked something in this discussion, but it is quite certain that this is the case. After filtering, PWM produces a defined fraction of the maximum output voltage of the pin, which is close, but not identical to the power supply voltage. If you want several devices to provide a well defined output voltage for a given input voltage, they should be powered by precision voltage regulators (of the same voltage, obviously).

The internal ADC "1.1 V" reference is stable, but is not a precision voltage source.

An alternative is to use an external DAC with a built in precision reference.

Finally, the TL072 is a poor choice for this circuit, since it is not a rail-to-rail op amp (on either the inputs or the output) intended for low voltage operation.

Thanks for the input.

Would something like a L7805 serve as a precision voltage reference? Or would I be looking into more specialized IC's?

Your right about the DAC, I have already built a couple of similar projects using an MCP4822 and it works great, but part of the reason I'm trying this is to get a better understanding of how to use the timers and to understand the "analog" voltage capabilities of these microcontrollers. (and I've already learned a bunch so :smiley: )

Regarding the tl072, Since I just took a quick screenshot of the schematic, I think it missed that the 072 is actually being powered from +/- 12v.

Look at a Voltage Reference
http://www.digikey.com/product-search/en/integrated-circuits-ics/pmic-voltage-reference/2556223?k=voltage%20reference

Such as ref194:
http://www.digikey.com/product-search/en/integrated-circuits-ics/pmic-voltage-reference/2556223?k=ref194

Did you realize that the PWM output gives you a fraction of whatever Vcc is?

So 255 is Vcc, not 5V. 127 is not 2.5V, it is 1/2 of Vcc.

You need to stabilize Vcc.

The clock speed will only affect the exact frequency of the PWM, not the percentage.

Did you know that the corner frequency of a lowpass RC filter with 10k and 100nF capacitor is 159Hz, and the PWM is only 490Hz? Response only drops by -20dB per decade or -6dB per octave. That is only about 1-1/2 octave. Are the following circuits OK with that much ripple? Keep in mind that is the power response, so for -6dB, that is only 1/2 the voltage ripple per octave. I'd estimate about -9dB attenuation at 490Hz.

This online RC calculator shows about -10dB attenuation at 500Hz (estimating on the Bode Diagram) with that 10k and 100nF filter.

http://sim.okawa-denshi.jp/en/CRtool.php

Perhaps the thing you are driving has its own lowpass filtering.

polymorph:
Did you realize that the PWM output gives you a fraction of whatever Vcc is?

So 255 is Vcc, not 5V. 127 is not 2.5V, it is 1/2 of Vcc.

You need to stabilize Vcc.

The clock speed will only affect the exact frequency of the PWM, not the percentage.

Did you know that the corner frequency of a lowpass RC filter with 10k and 100nF capacitor is 159Hz, and the PWM is only 490Hz? Response only drops by -20dB per decade or -6dB per octave. That is only about 1-1/2 octave. Are the following circuits OK with that much ripple? Keep in mind that is the power response, so for -6dB, that is only 1/2 the voltage ripple per octave. I'd estimate about -9dB attenuation at 490Hz.

This online RC calculator shows about -10dB attenuation at 500Hz (estimating on the Bode Diagram) with that 10k and 100nF filter.

(Sample)RC Low-pass Filter Design Tool - Result -

Perhaps the thing you are driving has its own lowpass filtering.

Aprreciate the help,

I think that understand the difference between VCC and 5v and their relationship to the voltage on the timer output pin. I was under the impression that the devices I was using for power (uno, usbattiny, external ps to arduino) would all go through a voltage regulator and that the output voltage would be similar enough that the differences wouldn't be noticeable.

I'm somewhat confused as to where the pwm being at 490hz came from?
If I have this set up properly then the pwm should be some percentage of timer0 which is 8 bit and running with no prescaler. My understanding of this is that the clock would be 16mhz/255 = 62.745khz?

I can't say for sure if this is the most ideal RC filter setup, but on my (somewhat cheap) oscope, I can't see (or hear) any ripple, so I'm ok with that aspect of it (for the moment at least :smiley: )

When 5V devices are powered by USB, Vcc will be the USB supply voltage and will differ from the voltage provided by the on board regulator. The regulators used on Arduinos are not precision and have significant variation from one board to the next.

490 Hz and 980 Hz are the frequencies used for AnalogWrite, depending on pin and Arduino type.

You are using a plastic film capacitor, not a ceramic, for the low pass filter I hope.