Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

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

  1. #1
    Junior Member
    Join Date
    May 2022
    Posts
    6

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

    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.

  2. #2
    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.

  3. #3
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    754
    so if you want an easy way to produce the analog signal, you can switch from Teensy 4.x to 3.x.
    Not particularly easy when you can't buy them!

  4. #4
    Well, yes, easy once you have one. Are T3.2 and LC available?

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,383
    Quote Originally Posted by joepasquariello View Post
    Are T3.2 and LC available?
    LC, yes.

    3.2, no.

  6. #6
    Junior Member
    Join Date
    May 2022
    Posts
    6
    Quote Originally Posted by joepasquariello View Post
    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?

  7. #7
    Quote Originally Posted by EugenM View Post
    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.

  8. #8
    Junior Member
    Join Date
    May 2022
    Posts
    6
    Quote Originally Posted by joepasquariello View Post
    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!

  9. #9
    Senior Member
    Join Date
    Oct 2020
    Posts
    175
    Maybe use mcp4725? The chip has i2c interface and is not hard to use even without a library. Here is a link:
    https://www.adafruit.com/product/935

  10. #10
    Senior Member
    Join Date
    Apr 2014
    Posts
    362
    OR......if you have plenty of spare pins use a R-2R ladder of resistors.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •