Teensy 4.1 how can you analogWrite() from 0v going up?

EugenM

Member
Greetings, im jus starting to learn how to use this wonderful board, i have found out that i need to output specific voltages through 1 wire, those voltages usually range between 100-500milivolts.

Naturaly i assumed that analogWrite() function would get the job done, however it semms that the smallest voltage is being outputed way to high.
This voltage ranges from 4.48volts AnalogWrite(24,0) to 1.18volts (AnalogWrite(24,254).

The smallest voltage of 1.18volts is way to high and im not even close of reaching 0v at any time.

I have tried to tinker with AnalogWriteRes(10) or even AnalogWriteRes(12) bits, increasing its stepping from 255 levels to 1024 and 4096 respectively for a finer control, yet none of those ever change the minimum voltage of 1.18v which is nowhere near the needed 100milivolts, let alone 0v.

Im sure im missing something important here, how can i actually get Teensy 4.1 to output through one of its pin from lets say 0v to 1v?

Thank you very much for all your help in advance!
Eugen.
 
In order to generate an analog voltage, you need a DAC output, which Teensy 4.x does not have. When you use AnalogWrite() on a digital pin, you are creating a PWM signal. The 2nd argument to the analogWrite() is the duty cycle, i.e. high time / period. The default resolution is 8 bits, so if you use analogWrite( pin, 128 ), you will get ~50% duty cycle, or a square wave. When you measure voltage of a PWM signal, depending on your meter, you will get an average voltage. You can use a PWM output to produce an analog signal by putting the PWM through an analog filter. If you search the forum you will find info on that.

The other alternative for Teensy 4.x is to use an external DAC, which is typically controlled via I2C or SPI. Teensy 3.x have actual DAC outputs, so if you want an easy way to produce the analog signal, you can switch from Teensy 4.x to 3.x.
 
In order to generate an analog voltage, you need a DAC output, which Teensy 4.x does not have. When you use AnalogWrite() on a digital pin, you are creating a PWM signal. The 2nd argument to the analogWrite() is the duty cycle, i.e. high time / period. The default resolution is 8 bits, so if you use analogWrite( pin, 128 ), you will get ~50% duty cycle, or a square wave. When you measure voltage of a PWM signal, depending on your meter, you will get an average voltage. You can use a PWM output to produce an analog signal by putting the PWM through an analog filter. If you search the forum you will find info on that.

The other alternative for Teensy 4.x is to use an external DAC, which is typically controlled via I2C or SPI. Teensy 3.x have actual DAC outputs, so if you want an easy way to produce the analog signal, you can switch from Teensy 4.x to 3.x.

Thank you very much for this information, since this new information, i wonder this hypothetical scenario, imagine if my 20dollar multimeter shows lets say a 1.18v on the PWM pin, since it actually goes up to 5v and then down to 0 very fast, would that mean that a PWM could fry the controler which can only accept real analog input of up to lets say 2v?

As for switching back to Teensy 3x that is not an option as they are out of stock everywhere i know, how about that low pass filter, is it slow? i need to switch the analog voltage like 1000 times a second, would that be a problem for said filter?
 
Thank you very much for this information, since this new information, i wonder this hypothetical scenario, imagine if my 20dollar multimeter shows lets say a 1.18v on the PWM pin, since it actually goes up to 5v and then down to 0 very fast, would that mean that a PWM could fry the controler which can only accept real analog input of up to lets say 2v?

As for switching back to Teensy 3x that is not an option as they are out of stock everywhere i know, how about that low pass filter, is it slow? i need to switch the analog voltage like 1000 times a second, would that be a problem for said filter?

T4.1 digital output signals are 3.3V, so a PWM signal is switching between ~0 and ~3.3V. What your meter shows will be a function of how it averages, but if you connect that PWM to something else (your controller), it will see 3.3V for short periods. If that device can "only accept 2V", then it could be damaged. If you google "analog from PWM", you will find lots of information. There are slower ways of doing it (RC filters) and faster ways (active components). If you want help, the best thing to do is say a little about what you're trying to do. If you just ask specific questions like this without context, you are unlikely to get the best advice.
 
T4.1 digital output signals are 3.3V, so a PWM signal is switching between ~0 and ~3.3V. What your meter shows will be a function of how it averages, but if you connect that PWM to something else (your controller), it will see 3.3V for short periods. If that device can "only accept 2V", then it could be damaged. If you google "analog from PWM", you will find lots of information. There are slower ways of doing it (RC filters) and faster ways (active components). If you want help, the best thing to do is say a little about what you're trying to do. If you just ask specific questions like this without context, you are unlikely to get the best advice.

Thank you very much for your help, you wont believe what happened... i was such in a hurry when i connected the pins that i actually used 5v instead of ground and reversed positive with negative so instead of it going from 0v to 3.3v it was going from 4.5v down to 1.18v

/facepalm/

Im glad nothing was damaged, everything works fine now.
Thank you very much!
 
Back
Top