SSD1306 OLED - sample code isn't working

Posted on
Page
of 2
/ 2
Next
  • Hi, I've used code to run ssd1306 display from Espruino Web but it has some error
    code I used

    function start(){
     // write some text
     g.drawString("Hello World!",2,2);
     // write to the screen
     g.flip(); 
    }
    
    // I2C
    I2C1.setup({scl:B6,sda:B7});
    var g = require("SSD1306").connect(I2C1, start);
    

    error I've got

    >echo(0);
    Uncaught InternalError: Timeout on I2C Write BUSY
     at line 1 col 19
    {b.writeTo(d,[0,a])}
                      ^
    in function called from line 1 col 147
    ...tion(a){b.writeTo(d,[0,a])});void 0!==c&&setTimeout(c,100);g...
                                  ^
    in function "connect" called from line 1 col 47
    var g = require("SSD1306").connect(I2C1, start);
                                                  ^
    =undefined
    >
    

    anyone knows how to fix it?

  • now I fixed timeouts, added 4,7k resistors from sda and scl to vdd, but can't see nothing on the screen, but Arduino code works and run display,

    fixed ;)
    usb socket cable wasn't keep pico strong enough to establish good connection

  • What size is your display? The default is for 128x64, but if you want to use 128x32 you have to use the code on the second half of the SSD1306 page.

  • can't find command for screen rotation in ssd1306 graphic library, is there any?

  • I think the graphics library has that?

    See the reference pages

  • Yes, it's literally just g.setRotation(...)

  • thank you for rotation tip,

    since couple days I wonder how to write proper code to make something like display few screens on the ssd1306, let say 3 of them, and every screen will display different values, I made simple code where espruino count how many times I press the button, then depend of the number will change the screen display, but there is some issue, until I put on the display some value once (without setInterval ) then it works fluently, but when I put setInterval() function (for analog readings) I have some lag, and can't switch between screens smoothly
    here is one of the codes I tried

    
    I2C2.setup({scl:B10,sda:B3});
    var g = require("SSD1306").connect(I2C2);
    
    g.clear();
    g.flip();
    
    
    var S1,S2;
    var BTN22 = B14;
    var BTN11 = B15;
    var screen = 0;
    
    g.setFontVector(20);
    
    pinMode(B14, 'input_pulldown');
    pinMode(B15, 'input_pulldown');
    
    function read(){
      g.clear();
      S1 = analogRead(A1);
      g.drawString("S1 " + S1.toFixed(3));
        g.flip();
    }
    
    function read2(){
        //clearInterval();
      g.clear();
      S2 = analogRead(A2);
      g.drawString("S2 " + S2.toFixed(3));
      g.flip();
    }
    
    function interval(){
      setInterval(read,500);
    }
    
    function interval2(){
      setInterval(read2,500);
    }
    
    function display(){
      if(screen === 1)interval();
      if(screen === 0)interval2();
      //console.log(screen);
    }
    
    setTimeout(function(){
      display();
    },2000);
    
    setWatch(function(e) {
      clearInterval();
      setTimeout(function(){
      display();
      console.log(screen);
      screen = screen + 1;
      if(screen > 1) screen = 0;
      },200);
    },BTN11, {repeat: true, edge: 'rising', debounce:50});
    

    where could be something wrong in my code?

  • What happens if you do:

    function display(){
      g.clear();
      g.drawString("Working...");
      g.flip();
      if(screen === 1)interval();
      if(screen === 0)interval2();
      //console.log(screen);
    }
    

    When you run setInterval it will only call the function after 500ms, and then every 500ms after that.

    Could that be explaining the delay you have?

  • in my code button didn't react for every press I did, after 8-9 times it stock for a while, and working again and then stock next time, would like to make this screen change smooth, no lags

  • I'm afraid I don't really know why that would be then... What happens when you uncomment //console.log(screen); - does it print the number every time you press the button, or does it stop when you start having problems with the button?

  • yes, numbers printed into console stop when I have the problem with the button

  • Are you sure it's not just an electrical problem with the way the button is wired up? What happens if you use the on-board button? Does it also have the problem?

  • I checked that, even if I use internal BTN it still stop after several times, sometimes after tens of times, then works again, and stock, still same, maybe is something wrong in the construction of my code?

  • any idea what can I do to get smooth change between screens?

  • You're still having the exact same problem with it?

  • yes, it stops after several dozen press of the button, then works again and next stops

  • Ahh - I just tried this, and it does appear to be a bug in Espruino.

    I believe it happens when you use clearInterval() (with no arguments) and debounce in a watch, as debounced watches use setTimeout behind the scenes. I'll get a fix in for this.

    In the mean time, you can work around it by only clearing the interval you want:

    // ...
    var intr;
    function interval(){
      intr = setInterval(read,500);
    }
    function interval2(){
      intr = setInterval(read2,500);
    }
    function display(){
      if(screen === 1)interval();
      if(screen === 0)interval2();
      //console.log(screen);
    }
    
    setTimeout(function(){
      display();
    },2000);
    
    setWatch(function(e) {
      if (intr) clearInterval(intr);
      intr = undefined;
    
      display();
      console.log(screen);
      screen = screen + 1;
      if(screen > 1) screen = 0;
    }, BTN1, {repeat: true, edge: 'rising', debounce:50});
    
  • Try using the binary from http://www.espruino.com/binaries/git/comĀ­mits/master/ in about half an hours' time and you should get a new firmware with it fixed

  • I think the usage of clearInterval() (with no arguments) is dangerous in general and should be avoided (except while playing in the Web IDE). Every module could use setInterval/setTimeout internally (and some do). I still like that clearInterval() does not remove the timer from setWatch anymore because it is a little unexpected.

    Or is there any safe usecase for clearInterval(void) which I don't see?

  • Well, at the start of onInit() it's good - if you've saved with any intervals running but wanted to clear them - same with clearWatch().

    In a small bit of code like above I don't think it's too bad as it won't interfere with anything else - but then again, if the code is that small, it's not too hard to keep track of intervals :)

  • thank you Gordon, will check it today later on with new firmware

  • Could anyone help to solve an issue with my code?
    I have 3 screens, first screen "one" on the beginning, then second screen "OLED OFF?" and third "analogRead(A5)"
    until I change screens by button (setWatch) and have no intervals set then it works really well, no lags, but when I want to update this analogRead(A5) informations every 500ms then I noticed that I can't switch to the other screen just by one push of the button, sometimes I need several or dozen push to do it, when I don't use intervals it works again,
    what should I change to be able to have few screens where I can watch on different analog values updated every 500-1000ms?

    
    
    function OLED(){
    clearWatch();
    
    I2C1.setup({scl:B6,sda:B7});
    var g = require("SSD1306").connect(I2C1);
    g.clear();
      g.flip();
      
    pinMode(B14, 'input_pulldown');
    
    var BTN22 = B15;
    var screen = 0;
    var battery = analogRead(A5)*2*3.3;
    
    function screens(){
      g.clear();
      g.setFontVector(15);
      g.drawString("one",25,20);
      g.flip();
    }
    
    function screens2(){
     g.clear();
     g.setFontVector(15);
     g.drawString("OLED OFF?");g.flip();
    }
    
    function screens3(){
      battery = analogRead(A5);
     g.clear();
     g.setFontVector(20);
     g.drawString("" + battery.toFixed(2),20,20);
     g.flip();
    }
    
    
    setWatch(function(e) {
      clearInterval();
        screen++;
        console.log(screen);
        if(screen === 0) screens();
      if(screen === 1) screens2();
      if(screen === 2) screens3();setIterval(screens3,500);
      if(screen >= 2) screen = 0;
    },BTN22, {repeat: true, edge: 'rising', debounce:150});
    
               
    OLED();
    
  • Are you using an up to date firmware (1v85)? You had some problem like this before (caused by a global clearInterval and I changed the firmware to help fix it).

  • I have version 1v85 and it isn't work, I have that error I described above

  • You know you've misspelt setInterval as setIterval, and haven't put brackets around functions that I think you expect to be executed in the same 'if' statement?

    if(screen === 2) screens3();setIterval(screens3,500);
    

    When you upload, do you not get any errors reported on the console? If you do, could you post them up? They might explain why it's not working.

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

SSD1306 OLED - sample code isn't working

Posted by Avatar for bigplik @bigplik

Actions