Arduino delayMicroseconds equivalent

Posted on
  • I've been "porting" an Arduino code example and been successfully converting it so far (not much to change other than removing the datatypes) and the code uses delayMicroseconds. Is there a similar way to achieve this in Espruino?

    The code is basically:

    method() {
      digitalWrite(pin, 1);
      digitalWrite(pin, 0);
  • At the moment there isn't a direct replacement. You can do:

    digitalWrite(pin, 1);
    setTimeout(function() {
      digitalWrite(pin, 0);
      setTimeout(function() {
      }, 0.35);

    or you can do:


    Which will be substantially more accurate. This actually starts executing subsequent commands (apart from other digitalPulses) before the pulse has ended, so for something more accurate, you could try:


    I'm trying to discourage people from writing code that delays (because then they start to get annoyed that Espruino isn't multithreaded), however I think delays do need to be added for this kind of thing - I've put a bug in for it, so hopefully I'll get around to it at some point.

  • Awesome! Worked like a charm! Thank you!

    Now I can toggle an RF-controlled light switch using my HY board and Espruino \o/

  • Great! Could you post up some details about it? I'd love to get a list of 'stuff you can do' going :)

  • Sure. A tiny disclaimer though, I don't know anything about this, I'm learning about microcontrollers as I go ;) Therefore, I might use the wrong terminology :)

    My board is the HYSTM32 3.2". It's currently running the 1v41 of Espruino (if that matters).
    I have two separate modules for RF signals, one receiver and one transmitter. Sorry for these links leading to a swedish shop, they can probably be found in other online stores as well (probably cheaper too).

    Any way.

    I soldered some headers onto my board so that I easily can connect and disconnect stuff while learning about these things. I tried connecting the modules using a breadboard, but the antenna pin seems to act up when doing so. So I just use male-female connector wires from the transmitter into the board. Connected ground and power (3v3) and the data pin to A2.

    I had found examples online to receive and transmit rf signals, but they were either for arduino or for raspberry pi. I ended up using the rc-switch library as a base.

    The example TypeA_WithDIPSwitches_Lightweight seemed to be the easiest way to start. My cheap RF-plugs is using this setup, 5dip on the remote and 2x5 dip on the plug. This example is more or less directly translatable into Espruino. Though I tweaked it a little bit:

    var thePin = A2;
    function send(numberHigh, numberLow) {
      digitalPulse(thePin, 1, 0.35 * numberHigh);
      digitalPulse(thePin, 0, 0.35 * numberLow);
    function sendCode(code) {
      for (repeat=0; repeat < 6; repeat++) {
        for (i=4; i < 16; i++) {
          send(1, 3);
          if (((code << (i-4)) & 2048) > 0) {
            send(1, 3);
          } else {
            send(3, 1);
    function on() {
    function off() {

    As my plugs and remote is set with the following system code:

    1   2    3   4    5
    on  off  on  off  on

    and the plug in the above code has the following dips set:

    a    b    c    d    e
    off  on   off  off  off

    I therefore use the code 1010101000 and append 10 for turning on and 01 for turning off.

    Theres a pretty good explanation on the rc-switch wikipages for how these plugs operate.

    My next goal is to port the receiving code so that my rf-receiver can react to the remote that comes along with the plugs.

  • Hi,

    Thanks, this is awesome! So you can now control all your plugs from the touchscreen?

    I actually bought a 433Mhz TX/RX pair a few days ago because I wanted to be able to read my heating oil level, but this is really neat. Do you have a link to the plugs/remote that you used?

    The receiving code might be a bit tricky - I'm not sure, but when I looked at the signal from my receiver there was a huge amount of data being sent (maybe it's where I live). I think it's possible that the amount of data (4,000 pulses/sec non-stop) might be too much for Espruino at the moment :(

  • In theory, yes! I bought them at a cheap store here in Sweden quite some time ago so do't have a link for them, sorry. They are just some "noname" super cheap versions.

    I noticed the spam in the receiver as well, haven't gotten that far yet though, got stuck experimenting with a gui on the touchscreen :)

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

Arduino delayMicroseconds equivalent

Posted by Avatar for Mudderman @Mudderman