1kHtz?

Posted on
  • In theory, the toggle function in the code below would create a PWM with a 1kHtz frequency with a 50% duty cycle, but I know there are some limitations to the hardware, has anybody tried something like this before?

    function myFunction() {
      var pin,High,Low;
      if (arguments[0]) pin = arguments[0];
      else pin = LED1;
      
      if (arguments[1]) High = arguments[1]; 
      else High = 1000;
      
      if (arguments[2]) Low = arguments[2];
      else Low = 1000;
      
      function toggle () {
        if (obj.on) {
          obj.toggle = setTimeout(toggle,Low);
          obj.on = false;
          digitalWrite(pin,0);
        } else {
          obj.toggle = setTimeout(toggle,High);
          obj.on = true;
          digitalWrite(pin,1);
        }
      }
      
      var obj = {
        on: true,
        toggle: setTimeout(toggle,High)
      };
    }
    
    myFunction(C11,1,1);
    
  • You can get that on a pwm pin real easy...

    analogWrite(pin, 0.5, {freq:1000})

  • Do you know the highest frequency that can be achieved with analogWrite?

  • Apparently 25 pins are capable of PWM, thanks!

  • I'm not 100% sure on the highest frequency, but I believe it is over 1MHz.

    My guess is that based on Espruino's execution speed, your code might struggle to run at 1KHz.

    You can definitely do a bit better with software if you need to (using setInterval and digitalPulse) but for things like PWM using the hardware wherever possible is preferable.

  • You can also get a good idea of which pins you can use on this page

  • The reason why I'm not using setInterval is because it would work for LED's but when I tried using it with a toy motor, it would turn on but not off... setTimeout works for the motor and LED's so I've just been using that

    In case it's not obvious, lol, I'm super new and just learning as I go.

  • Ahh, well what you'd want to do with setInterval is something like this:

    setInterval(function() {
      digitalWrite(pin,1);
      setTimeout(function() {
        digitalWrite(pin,0);
      }, 2); // 2ms @ 100Hz = 20%
    }, 10); // 100 Hz
    

    (So you use a setInterval for each full cycle, and then setTimeout for each pulse)

    Or you could use the built-in digitalPulse:

    setInterval(function() {
      digitalPulse(pin,1,2); // 2ms @ 100Hz = 20%
    }, 10); // 100 Hz
    

    Hopefully those should work on anything

  • Which pwm timers are assign to which pins?

  • All the pins that are marked with a PWM are compatible

    http://www.espruino.com/ReferenceESPRUIN­OBOARD

  • I just found this page

    http://www.espruino.com/Waveform

    which says espruino can only go up to .5KHtz with analogWrite()

  • Hover your mouse over the pins on the reference page and it will show which timers they're on. As for which ones Espruino uses (in cases where there are more than one timer on a pin), I think my list here is accurate: http://forum.espruino.com/comments/12102­383/ (Avoid using B4/B5, as noted in that thread)

    The Waveform page is describing outputting analog voltages using analogWrite() on a DAC pin (there are 2 DAC pins on Espruino board, A4 and A5 - output an actual analog voltage between 0 and 3.3v, instead of PWM), not a PWM pin.

  • I've been trying to replicate the voltage doubler (tripler actually) that is presented here:

    http://youtu.be/I4ED_8cuVTU?t=11m

    by alternating between

    analogWrite(C9, 0.5, {freq:1000})
    

    and

    analogWrite(pin, 0.5, {freq:5000})
    

    via the console but this isn't making a difference in voltage... I'm using 47uF Capacitors and regular diodes... I'll try replicating this again and sharing the result via video.

  • What he's suggesting there should work just fine with analogWrite. If you don't have an oscilloscope then maybe in order to check it's working you could set freq to 1 and attach an LED?

  • I've been trying that, and the voltage seems to be doubling, but I've wired it up to be tripled (which makes me believe it's only doubling because there's another input, not because there's added voltage from capacitors)... I've tested the difference by pulling out the second capacitor, but the brightness remains the same and changing the frequency has no difference from if I used the toggle function in the OP. I'll try uploading a test to youtube today and link it here.

  • If you have an oscilloscope it'd be a lot easier to see what was happening...

    I've just tried here and with two capacitors and two diodes I can get around 6v, which is about what you'd expect from the 3.3v output (minus some diode voltage drops).

    Because of the way it works, increasing the frequency won't increase the voltage at all - it'll just increase the current, but only up to as much as the 20mA STM32 output can supply.

  • Just to add that yes, it does work as a tripler. I get 8.64v out of it with the 4 caps and 4 diodes from his circuit.

    Are you sure you've wired it up right? I did mine wrong the first time (connecting the diode from the second set of capacitors to 3.3v not the output of the previous voltage doubler), and I just got 6v off it again.

  • Got distracted with something else, tomorrow for sure. Thanks for the help so far

  • http://youtu.be/BM3QU42jGsU

    The test leads are measuring the second capacitor from the PWM line, the schematic I've replicated can be found here: http://youtu.be/I4ED_8cuVTU?t=11m11s

    There is actually a drop in voltage from 5000 Htz to 10000 Htz and I just noticed a weird thing where the Capacitor's Voltage actually increases when you use digitalWrite(C9,0) and will only start discharging when you use digitalRead(C9)... also, if you look at the line of code I ran just before running the first digitalRead(C9), you will see that the output was 0 and yet I got a 1 on that pre-test... no idea what was going on there...

    I'm using 47uF capacitors, diodes that have a .7 forward voltage drop, 10K potentiometer and a 76ohm resistor

  • It looks to me like you're maybe drawing too much power out of it. Where is the LED connected?

    What happens if you just totally disconnect the potentiometer and the LED and then look at the voltage that you get?

  • The LED and potentiometer are connected where they should be receiving somewhere near 11V after you subtract the voltage drop from the diodes and how it's around a 4.7V output (the multimeter is monitoring the voltage of the second capacitor, not the total voltage of the circuit, when I measure the total voltage, it's just showing up as 4.7V). I've tried removing the LED and potentiometer and the capacitor just slowly discharges.

    Do you know why the voltage wouldn't drop until I used a digitalRead(C9)? Shouldn't just calling the digitalWrite(C9,0) cut the signal?

  • Did you ever set pinMode(pin,"mode") of C9 to "output"?

    If not, it is still on automatic setting mode, which means that a write makes it an output, and a read makes it an input... and as soon the read happens, the 'feeding' of your circuitry with power stops.

    Furthermore, I think, you may need to put a power driver in there... either a PNP or PMOS thing to get more juice to the circuitry while pin is high (active switching/feed). This allows you then also to feed directly from the battery - or even higher voltage source, if you desire so. Higher voltage source requires a little different circuitry (pin low, passive switching/feed).

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

1kHtz?

Posted by Avatar for user52526 @user52526

Actions