Startup time of arduino pro mini with mysensors 2.2 - took 10 sec (guess its the radio), can I execute some code immediately (<1sec)?

  • Hi,

    just encounter an issue I need help on. I'm new to mysensors . Try to upgrade some of my LEDs with DimmableLED Sensors. I'm using the current development branch for 2.2 as I had issues with the radio on 2.1.

    All fine - I created a small ardunio mini with the radio and and MOSFET for switching, works. But then I encounter an issue: every time I switch on the led with the wall switch it tooks a few seconds >10 to get ON. I want it immediately to go ON with 100%, then getting old value from gateway. I tested with a simple sketch without mysensors - it switches almost immediately , especially without bootloader.

    So I guess this is related to the startup time of the mysensors lib, any way to place some code somewhere it get executed ASAP?

    Like "analogWrite( LED_PIN, 255);" to switch LED ON in milisecs, not secs?


  • @alterfritz ... one thing to add: of cause I tried adding it 1st line in setup(), does not work.

  • @alterfritz said in Startup time of arduino pro mini with mysensors 2.2 - took 10 sec (guess its the radio), can I execute some code immediately (<1sec)?:

    d with the w

    If I correctly understood your setup you have wall switch on the high AC voltage side ? why not make it on the low voltage side ? the arduino can be powered all the time and your problem is gone.

  • In order to try help you find out... please share your sketch, serial output... which radio are you using... what makes you think it's the radio?

  • @rozpruwacz its a normal wall switch, using 220v. The LED using a ac-dc converter with 5v output. I use pro mini with 3.3v on raw input. seesm to work, however when I switch on it took around 10 sec, even I place analogWrite( LED_PIN, 255); 1st line in setup().

  • @manutremo

    here it is - mostly the example... I'm using NRF24L01. Why the radio - I can check messages on gateway / mqtt (on raspPi). Took them around 10 sec to show up...


    • The MySensors Arduino library handles the wireless radio link and protocol
    • between your home built sensors/actuators and HA controller of choice.
    • The sensors forms a self healing radio network with optional repeaters. Each
    • repeater and gateway builds a routing tables in EEPROM which keeps track of the
    • network topology allowing messages to be routed to nodes.
    • Created by Henrik Ekblad
    • Copyright (C) 2013-2015 Sensnology AB
    • Full contributor list:
    • Documentation:
    • Support Forum:
    • This program is free software; you can redistribute it and/or
    • modify it under the terms of the GNU General Public License
    • version 2 as published by the Free Software Foundation.

    • Version 1.0 - February 15, 2014 - Bruce Lacey
    • Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
    • This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
    • Vera Arduino Sensor project.
    • Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
    • The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
    • The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
    • to the LED negative terminal and the MOSFET Source pin is connected to ground.
    • This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    //#define MY_NODE_ID

    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95

    #include <MySensors.h>

    #define EPROM_LIGHT_STATE 1
    #define EPROM_DIMMER_LEVEL 2
    #define LIGHT_OFF 0
    #define LIGHT_ON 1

    #define SN "WallLight6"
    #define SV "1.0"
    #define CHILD_ID_LIGHT 1
    #define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
    #define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)

    static int16_t currentLevel = 0; // Current dim level...
    MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
    MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT);

    int16_t LastLightState=LIGHT_OFF;
    int16_t LastDimValue=100;


    • Dimmable LED initialization method
      void setup()
      // Pull the gateway's current dim level - restore light level upon sendor node power-up
      //request( CHILD_ID_LIGHT, V_DIMMER );
      //Serial.println( "Node ready to receive messages..." );

      int LightState=loadState(EPROM_LIGHT_STATE);
      if (LightState<=1) {
      int DimValue=loadState(EPROM_DIMMER_LEVEL);
      if ((DimValue>0)&&(DimValue<=100)) {
      //There should be no Dim value of 0, this would mean LIGHT_OFF

    //Here you actualy switch on/off the light with the last known dim level

    Serial.println( "Node ready to receive messages..." );


    void presentation()
    // Register the LED Dimmable Light with the gateway
    sendSketchInfo(SN, SV);

    present( CHILD_ID_LIGHT, S_DIMMER );



    • Dimmable LED main processing loop
      void loop()

    void receive(const MyMessage &message)
    if (message.type == V_LIGHT || message.type == V_DIMMER) {

    	//  Retrieve the power or dim level from the incoming request message
    	int requestedLevel = atoi( );
    	// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
    	requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
    	// Clip incoming level to valid range of 0 to 100
    	requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
    	requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
    	Serial.print( "Changing level to " );
    	Serial.print( requestedLevel );
    	Serial.print( ", from " );
    	Serial.println( currentLevel );
    	fadeToLevel( requestedLevel );
    saveState(EPROM_DIMMER_LEVEL, requestedLevel);
    	// Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
    	send(lightMsg.set(currentLevel > 0));
    	// hek comment: Is this really nessesary?
    	send( dimmerMsg.set(currentLevel) );



    • This method provides a graceful fade up/down effect
      void fadeToLevel( int toLevel )

      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;

      while ( currentLevel != toLevel ) {
      currentLevel += delta;
      analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
      delay( FADE_DELAY );

    void SetCurrentState2Hardware()
    if (LastLightState==LIGHT_OFF) {
    Serial.println( "Light state: OFF" );
    } else {
    Serial.print( "Light state: ON, Level: " );
    Serial.println( LastDimValue );

    //Send current state to the controller

    void SendCurrentState2Controller()
    if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) {
    } else {

  • and why You choosed that design ? you are not able to switch on the lights remotely.

  • @rozpruwacz This is the way it was constructed. I plan to leave them on all time, maybe remove / replace them in the future - however if somebody uses the switches the lights should go on instantly, basically by set analogWrite( LED_PIN,255); like a normal switch.

    Any way to do so?

  • @alterfritz
    Try to add the "on" command not in setup() (this requires presetation() to be already passed) and use a before()-routine instead.

  • So I would rather make it in hardware. pull-up resistor on the switching transistor will make it on at the powerup. and I would store the last dimm value into eeprom to not wait for the gateway response because you will not make it response in less that 1s - the protocol just not allow for that.

  • @rozpruwacz Exactly. This is why the "on" should be issued in before() (or something like hwInit()).
    "before()" is executed before any MySensors-specific communication....

  • In addition to the other advices you're already receiving, and following your suspicion that the delay might be related to the radio, you could try adding the following defines to your sketch:

    #define MY_PARENT_NODE_ID 0

    The first two avoid the node from having to search for it.

    The last one might help you determine if the problem is the radio not starting correctly if the time is reduced after you add it.

    Let us know how it goes.

  • yes, so in the before() function read the eeprom value and set it in analogWrite. hardware pull-up is optional because the delay between switching power on and before function call will be short enough, bit if You use the pull-up the delay will be even shorter.

  • @rejoe2 like this... btw - this forum is fanastic... responses more or less in real time... 🙂

    void presentation()
    analogWrite( LED_PIN,255);


  • @alterfritz

    sorry ... mean before this

    void before()
    analogWrite( LED_PIN,255);


    This seems to have done the trick... LED switch on immediately! Thanks!

    Btw... I also try the mysbootloader. This wirless update is great, however - does this bootloader slows down bootup time?

  • yes, because the code that will run in the node must be first uploaded to it. the bootup time will be dependent on the code size.

  • @alterfritz Yes, update-bootloader tries to connect to GW for updates first seconds (around 20). So it is not dependend on code size...
    Additional note: Waiting for RF connection to be established can also be prevented by using


  • @rejoe2

    Will wait for 1 sec?

  • @alterfritz Exactly. After trying to connect to the controller for 1 sec., the node will execute normal code like any other "just-arduino".
    Pls note: as there is no connection to the controller, the node may miss relevant info typically provided by the controller. Things like myControllerConfig, last values for counters etc... If you use this, you should make sure, these are set correctly later on.

  • So - thanks for all your help. Great forum. Let me summarize what I understand so far:

    1.) by using the before() procedbure I can execute immediately. Quickly tested. Seems perfect for me... however... 🙂
    2.) I really like this mysbootloader. But by using this it always takes up around 20 sec to boot up. There is NO way to switch on my LED directly when I power it up...


  • @alterfritz Speaking about software: Not exactly, there is one thing you could still do: Compile the bootloader yourself with light switched on... 😀

  • @rejoe2 Right... 🙂 I may look into this. For now I will try to get a stable version and load it without bootloader directly via usbasp...

Log in to reply

Suggested Topics

  • 3
  • 10
  • 3
  • 24
  • 2
  • 2