Bra!nPa!N
Posts: 4
Joined: Fri Jul 13, 2018 5:32 am

Turn an OLED-Displa off/on

Fri Jul 13, 2018 6:02 am

Hello everbody,

I'm a raspberry pi beginner and working on a camera controller with an OLED-Display using Python and Adafruits SSD1306 library. Everything works fine and I was even able to program a menu in python which is controllable via buttons. So far so good.

As I want to save energy while capturing a timelapse I want to turn off the display with an separate button. Pushing the button again should turn the Display on showing the last Contents. The button works fine but I can't find any command to tell the button what to do.

Does anyone of you have an idea how to solve the problem?

Thanks a lot in advance.

Regards from Germany
BrA!nPa!N
"I'm a very stable genius" (Trump) :lol:

aBUGSworstnightmare
Posts: 766
Joined: Tue Jun 30, 2015 1:35 pm

Re: Turn an OLED-Displa off/on

Fri Jul 13, 2018 12:28 pm

SSD1306 has a command for switching display on/off. On a press of the button you need to toggle your displays power state

Bra!nPa!N
Posts: 4
Joined: Fri Jul 13, 2018 5:32 am

Re: Turn an OLED-Displa off/on

Fri Jul 13, 2018 1:00 pm

The Problem is, that I can't find any Python-code to Switch the display off/on.
"I'm a very stable genius" (Trump) :lol:

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 4523
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Turn an OLED-Displa off/on

Fri Jul 13, 2018 2:04 pm

Bra!nPa!N wrote:
Fri Jul 13, 2018 1:00 pm
The Problem is, that I can't find any Python-code to Switch the display off/on.
FWIW below is an extract** for 'C' defines associated with the hex (command) code(s) associated with a SSD1306 display:

Code: Select all

#define OLED_CONTRAST_RESET       0x7f
#define OLED_HORIZONTAL_ADDR_MODE 0x00
#define OLED_VERTICAL_ADDR_MODE   0x01
#define OLED_PAGE_ADDR_MODE       0x02
#define OLED_COLUMN_START_RESET   0
#define OLED_COLUMN_END_RESET     127
#define OLED_PAGE_START_RESET     0
#define OLED_PAGE_END_RESET       7
#define OLED_CHAR_WIDTH           128 /* 8x8 font */

/* Single-byte Commands ... */
#define OLED_DISPLAY_ON_WITH_RAM 0xa4 
#define OLED_DISPLAY_ON_NO_RAM   0xa5
#define OLED_DISPLAY_NORMAL      0xa6 
#define OLED_DISPLAY_INVERSE     0xa7 
#define OLED_DISPLAY_OFF         0xae
#define OLED_DISPLAY_ON          0xaf
#define OLED_SET_SEG0_0          0xa0
#define OLED_SET_SEG0_127        0xa1 /* Re-map */
#define OLED_SET_COMO_NORMAL     0xC0
#define OLED_SET_COMO_REMAP      0xC8

/* Double-byte Command prefixes ... */
#define OLED_SET_CONTRAST        0x81
#define OLED_SET_MEM_ADDR_MODE   0x20

/* Triple-byte Command prefixes ... */
#define OLED_SET_COLUMN_ADDR     0x21
#define OLED_SET_PAGE_ADDR       0x22

/* Multi-byte Command prefixes ... */

/* Contrast ... */
#define OLED_C0 0x0f
#define OLED_C1 0x1f
#define OLED_C2 0x3f
#define OLED_C3 0x5f
#define OLED_C4 OLED_CONTRAST_RESET
#define OLED_C5 0x9f
#define OLED_C6 0xbf
#define OLED_C7 0xdf
#define OLED_C8 0xef
I'm guessing that your python code already uses some of the above (hex) values (or their decimal equivalent) and can be adapted to send either the OLED_DISPLAY_ON_WITH_RAM, OLED_DISPLAY_ON_NO_RAM or OLED_DISPLAY_OFF values to the display as required. (Google the SSD1306 datasheet for more info.)
Trev.
** http://www.cpmspectrepi.uk/raspberry_pi ... 2c_chips.h
Still running Raspbian Jessie on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W) but Stretch on my P3B+, P3B, B+, A+ and a B2. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Bra!nPa!N
Posts: 4
Joined: Fri Jul 13, 2018 5:32 am

Re: Turn an OLED-Displa off/on

Mon Jul 16, 2018 6:24 am

I am using the Adafruit ssd1306 library and I found the following within the main-file "ssd1306.py" of the library:

Code: Select all

# Constants 
SSD1306_I2C_ADDRESS = 0x3C    # 011110+SA0+RW - 0x3C or 0x3D 
SSD1306_SETCONTRAST = 0x81 
SSD1306_DISPLAYALLON_RESUME = 0xA4 
SSD1306_DISPLAYALLON = 0xA5 
SSD1306_NORMALDISPLAY = 0xA6 
SSD1306_INVERTDISPLAY = 0xA7 
SSD1306_DISPLAYOFF = 0xAE 
SSD1306_DISPLAYON = 0xAF
....
Unfortunately I have no idea how to implement the variables "SSD1306_DISPLAYOFF" and "SSD1306_DISPLAYON" in my own python code :( Do you have an idea how to do it? As I am an absolutly beginner I did not realy understand the ssd1306-documentation.
"I'm a very stable genius" (Trump) :lol:

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 4523
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Turn an OLED-Displa off/on

Mon Jul 16, 2018 12:21 pm

Bra!nPa!N wrote:
Mon Jul 16, 2018 6:24 am
I am using the Adafruit ssd1306 library and I found the following within the main-file "ssd1306.py" of the library:

Code: Select all

# Constants 
...
SSD1306_DISPLAYALLON_RESUME = 0xA4 
SSD1306_DISPLAYALLON = 0xA5 
SSD1306_NORMALDISPLAY = 0xA6 
....
Unfortunately I have no idea how to implement the variables "SSD1306_DISPLAYOFF" and "SSD1306_DISPLAYON" in my own python code :( Do you have an idea how to do it? As I am an absolutly beginner I did not realy understand the ssd1306-documentation.
The constants above are your (python) equivalent to my ('C) ones:

Code: Select all

#define OLED_DISPLAY_ON_WITH_RAM 0xa4 
#define OLED_DISPLAY_ON_NO_RAM   0xa5
#define OLED_DISPLAY_NORMAL      0xa6 
They're not variables but "parameters" you can use in a python function call instead of the (hex) values directly. You'll need to look through any python code you've been provided in the library with to find that function.
Trev.
Still running Raspbian Jessie on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W) but Stretch on my P3B+, P3B, B+, A+ and a B2. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

DirkS
Posts: 8470
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Turn an OLED-Displa off/on

Mon Jul 16, 2018 1:50 pm

FTrevorGowen wrote:
Mon Jul 16, 2018 12:21 pm

Code: Select all

#define OLED_DISPLAY_ON_WITH_RAM 0xa4 
#define OLED_DISPLAY_ON_NO_RAM   0xa5
#define OLED_DISPLAY_NORMAL      0xa6 
They're not variables but "parameters" you can use in a python function call instead of the (hex) values directly. You'll need to look through any python code you've been provided in the library with to find that function.
Trev.
I had a quick look on the webs and it may not be enough to just send these commands

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 4523
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Turn an OLED-Displa off/on

Mon Jul 16, 2018 2:07 pm

DirkS wrote:
Mon Jul 16, 2018 1:50 pm
FTrevorGowen wrote:
Mon Jul 16, 2018 12:21 pm

Code: Select all

#define OLED_DISPLAY_ON_WITH_RAM 0xa4 
#define OLED_DISPLAY_ON_NO_RAM   0xa5
#define OLED_DISPLAY_NORMAL      0xa6 
They're not variables but "parameters" you can use in a python function call instead of the (hex) values directly. You'll need to look through any python code you've been provided in the library with to find that function.
Trev.
I had a quick look on the webs and it may not be enough to just send these commands
To fully initiase a display I agree but, since the OP asked how to (just) turn a (I presumed initialised, by the library code) display on or off those should work (or, rather they have for me, from the command line or via 'C' code but, then again, only one of my displays has ever worked, and its "board layout" is different to those I've tried later :( )
Trev.
Still running Raspbian Jessie on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W) but Stretch on my P3B+, P3B, B+, A+ and a B2. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

Bra!nPa!N
Posts: 4
Joined: Fri Jul 13, 2018 5:32 am

Re: Turn an OLED-Displa off/on

Tue Jul 17, 2018 5:55 am

Thank you Trev, you are right. It's the "command"-function in the library which can be used. The following code will turn the disply off/on:

Code: Select all

# turn display off
disp.command(Adafruit_SSD1306.SSD1306_DISPLAYOFF)
#turn display on
disp.command(Adafruit_SSD1306.SSD1306_DISPLAYON)
At the moment I am just to stupid to put it in a working if-loop. But this will be a solvable problem =)

Thanks a lot for your help!
"I'm a very stable genius" (Trump) :lol:

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: No registered users and 7 guests