onInit with HD44780

Posted on
  • Beginner with espruino and having some trouble with onInit on my Pico:

    function onInit() {
        I2C1.setup({scl:B6, sda:B7});
        var lcd = require("HD44780").connectI2C(I2C1);
        lcd.clear();
        lcd.print( "Hello World!" );
    }
    

    Upload and save() works as expected:

    >reset();
    =undefined
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v88 Copyright 2016 G.Williams
    >echo(0);
    =undefined
    >save()
    =undefined
    Erasing Flash.....
    Writing.......
    Compressed 81600 bytes to 4445
    Checking...
    Done!
    Running onInit()...
    

    But when I un- and re-plug USB it's not working - the LCD is staying blank:

    Disconnected
    Connected
    >Loading 4445 bytes from flash...
    Running onInit()...
    =undefined
    Uncaught InternalError: Timeout on I2C Write Transmit Mode 2
     at line 2 col 13
    g|4,g|4,g,g])
                ^
    in function "a" called from line 1 col 7
    a(51,1);a(50,1);a(40,1);a(12,1);a(6,1);a­(1,1);return{write:a...
          ^
    in function "h" called from line 2 col 15
    g|4,g|4,g,g])})
                  ^
    in function "connectI2C" called from line 5 col 49
        var lcd = require("HD44780").connectI2C(I2C1);
                                                    ^
    in function called from system
    =undefined
    Execution Interrupted during event processing.
    

    Just manually executing load() again and voila - the LCD shows "Hello World!"

    >load()
    =undefined
    Loading 4445 bytes from flash...
    Running onInit()...
    

    What's going wrong?

  • The display may need a moment to I utilize itself, try waiting a second or two before you try to use it (by putting the initialization code in a setTimeout()

  • Sorry, no. Tried this before.
    Even 2 seconds don't make a change:

    function onInit() {
      setTimeout( function() {
        I2C1.setup({scl:B6, sda:B7});
        var lcd = require("HD44780").connectI2C(I2C1);
        lcd.clear();
        lcd.print( "Hello World!" );
      }, 2000 );
    }
    

    I even use the very same LCD in an Arduino project and can start sending to it right away with no issues there.

    Any other idea?

  • Suggest trying I2C.setup and require lines before the settimeout.
    leave the lcd lines inside the settimeout

  • The .connect() writes initialization code(*) to the LCD... keeping this initialization outside of (/ execute before) the delaying timeout() for the application driven .clear() and .print() should solve the issue with a much smaller delay (50ms)... which is what @ClearMemory041063 suggests.

    (*) HD44780.js module, lines 28 under '//initialise'. The *** Initializing by Instruction*** section in HD4480 Datasheet - on pages 45 and 46 - talks about timings when initialization instructions are sent... (datasheet pdf doc linked below). The times before the initialization instructions may not need to be taken into account, because proper power should have been stable before first JS code is reached.


    1 Attachment

  • B6 and B7 on the Pico are pins used for Serial1 - which is where the console moves by default.

    I guess that might be affecting things - while they shouldn't transmit anything for ~1 second after startup (by which time you changed the pins around anyway), I guess it's possible that their startup values confuse I2C on the display and it needs a timeout to recover.

    I'd hope that the following - as suggested above - might work:

    function onInit() {
      I2C1.setup({scl:B6, sda:B7});
      setTimeout( function() {
        var lcd = require("HD44780").connectI2C(I2C1);
        lcd.clear();
        lcd.print( "Hello World!" );
      }, 2000 );
    }
    

    But the other option is just to use some different pins - it's bad luck - those two are literally the only ones on the board that get used for something else by default :)

  • Gordon, it seems the pins caused it.
    Just changed it to I2C3 and this works:

    function onInit() {
      I2C3.setup({scl:A8, sda:B4});
      var lcd = require("HD44780").connectI2C(I2C3);
      lcd.clear();
      lcd.print( "Hello World!" );
    }
    

    No delay needed. Just different pins.

    Thanks for the hint.

  • I had a similar problem with an infrared temperature sensor a few month ago - sadly without finding the bug: http://forum.espruino.com/conversations/­280634/#comment12743705

    @ChristianW
    Could you try out the pin configuration I2C1.setup( { scl: B8, sda: B9 } );?
    This would be really nice and would help to verify/falsify my voodoo theory that I2C1 is different in some kind of initialisation and that the cause are not the concrete pins (B6, B7).

    For completeness: I have a lot of (>10) different I2C devices which work on I2C1 without any problem.

  • @luwar
    I2C1.setup( { scl: B8, sda: B9 } ); works - without any delay.
    Also right after save() and on power-up.

  • Thank you. So I know that my problem with the MLX90614 has obiously a different cause.

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

onInit with HD44780

Posted by Avatar for ChristianW @ChristianW

Actions