JPB-HK
Posts: 56
Joined: Thu Nov 22, 2012 5:00 pm

I2C with Adafruit 128x32 I2C OLED

Mon Dec 24, 2012 11:49 pm

I have been having problems with the I2C communications between the Raspberry Pi and Adafruit's 128x32 I2C OLED. Let me summarize what I have done so far:
With OLED connected to I2c port 1, can see that it is connected

Code: Select all

i2cdetect -y 1
shows 0x3c, the address of the oled, which tells me I have the right port as well as the right data / clock pins and power.

I have the permissions set for /dev/i2c-1 set to 0666, doing this in /lib/udev/rules.d/60-i2c-tools.rules

Code: Select all

KERNEL=="i2c-[1-9]*, GROUP="i2c", MODE="0666"
I have tried several different 'C' commands borrowing from all the different code sets out there. All compile with no problems and runs and sends data to the i2c sda/scl 1

Code: Select all

i2c_smbus_write_byte(deviceHandle, buff);
// OR
write(deviceHandle, buff, 1);
I don't receive any i2c write errors, and have tried direct cmd such as i2cset.
I have a scope monitoring sda/scl and the voltages and timing seems to be OK.
The data out does not seem to conform to the specs in the Solomon Systech SSD1306 device (8.1.5.2). I clearly see the 7 bit address of the device after the start bit (0x3C), then R/W set to W=0, then ACK from the OLED. From here the write cmd only sends Data bytes. But according to the spec, there should be a continuation bit, and a D/C# (data/command) bit. But all I ever see is just the address, R/W, and ACK with 8 bits of data.

I was hoping not to have to bit bang this, but is that my only recourse? The examples from Adafruit only shows how to interface with Arduino from the Raspberry Pi. The code example provided for the OLED is from an Arduino using the above commands.

Anyway, appreciate any comments and help, and if I left out something, let me know.
Thanks,
John
NCR 310 - ZX81 - Apple II+ - Dell/Intel - Mac mini... Raspberry Pi!

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: I2C with Adafruit 128x32 I2C OLED

Tue Dec 25, 2012 6:44 pm

Hello John,

I'm not sure how you have implemented your code, but I waiting for a similar display for my Raspberry Pi to arrive so I can do some experimenting.

Since the I2C addressing is working (you see the ACK), I would not expect difficulty with the rest of the protocol. You should not need to resort to bit banging.

You state:
I clearly see the 7 bit address of the device after the start bit (0x3C), then R/W set to W=0, then ACK from the OLED. From here the write cmd only sends Data bytes. But according to the spec, there should be a continuation bit, and a D/C# (data/command) bit. But all I ever see is just the address, R/W, and ACK with 8 bits of data.
The control byte with data/command and continuation bit is just another 8-bit data byte that you need to send at the front of your data stream. It is not part of the I2C protocol. Reviewing the AdaFruit_SSD1306 library for the Arduino shows the sending of this byte for an I2C transaction.

In ssd1306_command(), notice the control byte sent before the command, with D/C set to 0:

Code: Select all

    // I2C
    uint8_t control = 0x00;   // Co = 0, D/C = 0
    Wire.beginTransmission(_i2caddr);
    Wire.write(control);
    Wire.write(c);
    Wire.endTransmission();
And, in ssd1306_data(), notice the control byte sent before the data, with D/C set to 1:

Code: Select all

    // I2C
    uint8_t control = 0x40;   // Co = 0, D/C = 1
    Wire.beginTransmission(_i2caddr);
    Wire.write(control);
    Wire.write(c);
    Wire.endTransmission();
And, later for sending several data bytes, with D/C set to 1:

Code: Select all

      // send a bunch of data in one xmission
      Wire.beginTransmission(_i2caddr);
      Wire.write(0x40);
      for (uint8_t x=0; x<16; x++) {
        Wire.write(buffer[i]);
        i++;
      }
      i--;
      Wire.endTransmission();
Hope this is some help.

Enjoy!
Bill

JPB-HK
Posts: 56
Joined: Thu Nov 22, 2012 5:00 pm

Re: I2C with Adafruit 128x32 I2C OLED

Tue Dec 25, 2012 10:22 pm

Bill, Thanks, I totally missed that! I will give this a try tonight and post the results.
John
NCR 310 - ZX81 - Apple II+ - Dell/Intel - Mac mini... Raspberry Pi!

JPB-HK
Posts: 56
Joined: Thu Nov 22, 2012 5:00 pm

Re: I2C with Adafruit 128x32 I2C OLED

Wed Dec 26, 2012 1:13 pm

[Update] This did the trick! All is working well. I was just looking too hard at it. This is my first experience with i2c, so maybe I should read up on it a little more.

Now I get to clean up the code, will probably make a class for this in c++.

John
NCR 310 - ZX81 - Apple II+ - Dell/Intel - Mac mini... Raspberry Pi!

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: I2C with Adafruit 128x32 I2C OLED

Wed Dec 26, 2012 4:13 pm

Good to know you were able to get it working. I look forward to seeing a future post with the C++ class.

Enjoy!
Bill

JPB-HK
Posts: 56
Joined: Thu Nov 22, 2012 5:00 pm

Re: I2C with Adafruit 128x32 I2C OLED

Sat Dec 29, 2012 1:00 am

Have working code for the i2c interface to the OLED. It is text only, with 4 rows (0-3), and start position at 128 (0-127) different columns. The text will wrap around to the next row, but is not intelligent enough to move a partially displayed character. The code manages an image of the display in memory, and then writes the complete image to the display. The font comes from Adafruit's demo, but I moved it to a .h file. No graphics at this point and no scrolling support. That's for another time, maybe.

I have attached a compressed tar ball. My first one, so let me know if there are any problems. It should contain the code to run on the Raspberry Pi. The directory structure is such that all projects are is 1 directory away from the base directory. It makes the assumption that the i2c device already setup on your Raspi, and that you have I2c port 1 enabled for r/w.

I do get some i2c write errors occasionally, but not sure if this is from the exposed wiring. I did just change to 4 wires, with gnd between the two signals, see if that helps.
128x32i2c.tar.gz
(5.74 KiB) Downloaded 787 times
NCR 310 - ZX81 - Apple II+ - Dell/Intel - Mac mini... Raspberry Pi!

karl34
Posts: 2
Joined: Tue Mar 26, 2013 11:53 am

Re: I2C with Adafruit 128x32 I2C OLED

Tue Mar 26, 2013 12:07 pm

I have a question for you...when I had originally used this device with my Arduino, a pin was connected to RST. When you're using this with the RPI, do you connect the reset pin? Also, is power connected to 3.3v or VIN?

Thanks!

JPB-HK
Posts: 56
Joined: Thu Nov 22, 2012 5:00 pm

Re: I2C with Adafruit 128x32 I2C OLED

Wed Mar 27, 2013 5:08 am

Karl34:
I originally did not have RST connected, and this worked OK on the bench, but when I installed a display in a Raspberry Pi case, without a separate power supply for the display, I needed the RST to get the display to work. So I hooked RST up to Pin 7 (GPIO4) of the Raspberry Pi. I use Gertboard SW to drive the pin. This forced my program to be run with 'sudo'.
I don't have my case open, but I think I connected Pin 1 (3.3V) of the Raspberry Pi to the 3.3V on the OLED.

John
NCR 310 - ZX81 - Apple II+ - Dell/Intel - Mac mini... Raspberry Pi!

JPB-HK
Posts: 56
Joined: Thu Nov 22, 2012 5:00 pm

Re: I2C with Adafruit 128x32 I2C OLED

Wed Mar 27, 2013 5:54 am

Here is my latest I2c code in a class wrapper. In the cpp file I included simple instructions for use. It is a basic read /write package, an easy module to add to other projects. My file structure is simple to allow for projects to easily use previously created modules, such as i2c.

Code: Select all

[source]
    '- common
        '- i2c.cpp
        '- i2c.h
    '- tmp175
        '- tmp175.cpp
        '- tmp175.h
    '- [Project 1]
    '- [Project 2]
When developing a new project, and I want to include a common element, such as i2c, I add the .cpp file to the project, and I just include the relative path:

Code: Select all

#include "../common/i2c.h"
John
Attachments
130327i2c.tar.gz
(1.57 KiB) Downloaded 420 times
NCR 310 - ZX81 - Apple II+ - Dell/Intel - Mac mini... Raspberry Pi!

naicheben
Posts: 344
Joined: Sat Jan 28, 2012 12:28 pm
Contact: Website

Re: I2C with Adafruit 128x32 I2C OLED

Wed Mar 27, 2013 9:01 pm

Thank you for the code, I have successfully read from my magnetic sensor. Nice easy to use wrapper :D

karl34
Posts: 2
Joined: Tue Mar 26, 2013 11:53 am

Re: I2C with Adafruit 128x32 I2C OLED

Thu Mar 28, 2013 12:27 pm

Thank you for the info and the updated library. Very much appreciated.

User avatar
mike808
Posts: 21
Joined: Mon Jun 03, 2013 12:55 am

Re: I2C with Adafruit 128x32 I2C OLED

Sat Jun 08, 2013 11:27 pm

i should be getting my 128x64 OLED display (from Adafruit) on monday and i wanted to hook it up to the raspberry pi. from the tutorials they have, it's only for arduino and in python, but i want to use c++. wondering if you have examples using this api/library on how to do so? looks like the OLED driver can communicate with either I2C or SPI, but i'm a newbie when it comes to interfacing to hardware, but i have a passion to learn that, thus my project :)

JPB-HK
Posts: 56
Joined: Thu Nov 22, 2012 5:00 pm

Re: I2C with Adafruit 128x32 I2C OLED

Tue Jun 11, 2013 6:14 am

mike808 wrote:i should be getting my 128x64 OLED display (from Adafruit) on monday and i wanted to hook it up to the raspberry pi. from the tutorials they have, it's only for arduino and in python, but i want to use c++. wondering if you have examples using this api/library on how to do so? looks like the OLED driver can communicate with either I2C or SPI, but i'm a newbie when it comes to interfacing to hardware, but i have a passion to learn that, thus my project :)
mike808:
The latest i2c code is posted in this thread, although I do not have any posting for the oled driver itself. You will want to get familiar with how the oled is initiated and I think Adafruit has a good example on their site. Even though it is for Arduino you can use it for Raspberry Pi as well. You will just need to pick through the code and find how they handle writing to the display and displaying fonts. I think they have a 5x7 font on their site. The only difference between the 32 and 64 is just that, and you can plug this number in when you initialize it the display. I think the i2c address is different too. Sorry, no definitive answers as I am away from my code at the moment.
One thing I noticed is that there are two ways of approaching writing to the display. One is to clear the display and then always write the to the full display. That was my first approach. The second, and what Adafruit does, is to only write to the area you want to write to. This is more difficult to understand, and so I had to do the first way on my initial try. Then I implemented this second approach later after I felt more comfortable with the display coding.
There are some good tutorials on this forum and other places that show how to get i2c started on the Raspberry Pi. Follow the step-by-step instructions. And sorry if this is basic info, I just know that when I was doing this earlier on, it was difficult to find info on the subject.
You can also look at Gordon's wiringPi source as well and use it I think with Gertboard code.
This has been a fun project for me, and helped me to start investigating not only Raspberry Pi, but also Linux.
I am sure you will have more questions as you move forward, we will try and help along the way.
John
NCR 310 - ZX81 - Apple II+ - Dell/Intel - Mac mini... Raspberry Pi!

User avatar
mike808
Posts: 21
Joined: Mon Jun 03, 2013 12:55 am

Re: I2C with Adafruit 128x32 I2C OLED

Thu Jun 13, 2013 1:57 am

John,

thank you very much for the explanation and helpful hints. this site (http://www.acmesystems.it/i2c) was also helpful too. i was able to get simple text to the lcd using I2C. like you, i am going to go the easy way first and always write to the full display. so far not too bad as i can follow the Adafruit implementation like you suggested and implement that for the pi.

took a quick break to say thank you, but now... back to the driver code! :D

Mike

User avatar
mike808
Posts: 21
Joined: Mon Jun 03, 2013 12:55 am

Re: I2C with Adafruit 128x32 I2C OLED

Wed Jun 19, 2013 2:45 am

I was able to get some simple text to display, but now wanted to dive a bit deeper and display a bitmap. I'm not sure how to send control bytes for START and STOP for the I2C bus on the raspberry pi. Does anyone know what I should be sending (hex value) and what the response looks like when the response comes back? I am under the impression I get some type of acknowledgement when either START or STOP is sent?

pseducode:

Code: Select all

send_i2c_start(); // tx control byte(s) for i2c START
wait_for_response(); // wait for response to control byte(s)
for (int i=0; i<bytes_to_send; i++)
{
    transmit_byte(buffer[i]);
}
send_i2c_stop(); // tx control byte(s) for i2c STOP
wait_for_response(); // wait for response to control byte(s)

Charly86
Posts: 23
Joined: Wed Mar 06, 2013 9:57 am

Re: I2C with Adafruit 128x32 I2C OLED

Wed Jul 03, 2013 12:31 pm

Hi Guys,

If you are interested there is a library for driving I2C/SPI Oled for Pi, you can check the tutorial here, the source code is fully available
http://hallard.me/adafruit-ssd1306-oled ... pberry-pi/

hope this help

User avatar
darrencuk
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 10
Joined: Fri Apr 27, 2012 3:48 pm
Location: Bolton, England

Re: I2C with Adafruit 128x32 I2C OLED

Wed Feb 05, 2014 12:46 pm

Hi

I have one of these I2C oled displays from adafruit but so far I have only managed to display the demo on it.

I have a DS1820 and would like to display the temperature on the screen in python but I am struggling finding any help or tutorials.

Could someone please help me and point me in the right direction. I would be happy if I could even get a simple 'Hello World!' to display :)

Thanks in advance

Darren
Raspberry Pi Certified Educator

User avatar
darrencuk
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 10
Joined: Fri Apr 27, 2012 3:48 pm
Location: Bolton, England

Re: I2C with Adafruit 128x32 I2C OLED

Tue Feb 11, 2014 8:27 pm

Hi

Please can nobody point me in the right direction.

Thanks.
Raspberry Pi Certified Educator

User avatar
mike808
Posts: 21
Joined: Mon Jun 03, 2013 12:55 am

Re: I2C with Adafruit 128x32 I2C OLED

Wed Feb 12, 2014 3:23 am

i posted my code on my blog.

http://pauhanafun.blogspot.com/2014/02/ ... nd-pi.html

it's written in c++ but you can modify to link in the python calls. you will need to adjust code as my module is a 128x64 adafruit display.

User avatar
darrencuk
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 10
Joined: Fri Apr 27, 2012 3:48 pm
Location: Bolton, England

Re: I2C with Adafruit 128x32 I2C OLED

Thu Feb 13, 2014 9:05 am

Hi Mike

Many thanks for the help, I will look into the code you've shared. I am a complete python newbie, so I may as well try and work in C++. Not sure if there are any benefits to this or not though.

Thanks again, I will post back how I get on!

Darren.
Raspberry Pi Certified Educator

darksheep
Posts: 2
Joined: Sun May 25, 2014 5:51 pm

Re: I2C with Adafruit 128x32 I2C OLED

Sun May 25, 2014 5:54 pm

This is really cool could you post some pics or may bean explanation of how you hooked up the screen to the pi.

I am looking at doing the same thing what version of the raspberry pi software are you using ?

User avatar
mike808
Posts: 21
Joined: Mon Jun 03, 2013 12:55 am

Re: I2C with Adafruit 128x32 I2C OLED

Tue May 27, 2014 2:49 am

I have posted pics on my blog. Here are a few:
http://pauhanafun.blogspot.com/2013/06/ ... using.html
http://pauhanafun.blogspot.com/2013/07/ ... d-lcd.html

Nothing special with the OS as it's running Raspbian Wheezy.

It's hooked up using I2C. Refer to the raspberry pi pin outs for more information.

itsisme
Posts: 20
Joined: Sat Mar 09, 2013 2:16 am

Re: I2C with Adafruit 128x32 I2C OLED

Sun Jun 29, 2014 1:15 pm

Hello,

I just received my oled from Ebay and trying to get it all compiled.
What is the command to compile it from the 128x32i2c.tar.gz file.

My Oled is an I2C one..

Hope to get some reply so I can use the module...

Chrs..

User avatar
mike808
Posts: 21
Joined: Mon Jun 03, 2013 12:55 am

Re: I2C with Adafruit 128x32 I2C OLED

Sun Jun 29, 2014 1:58 pm

it would help to know where you got that file from. you need to first untar it (tar -xzf 128x32i2c.tar.gz). is there a makefile there? if so just run make. if not, what are the contents? any readme?

itsisme
Posts: 20
Joined: Sat Mar 09, 2013 2:16 am

Re: I2C with Adafruit 128x32 I2C OLED

Sun Jun 29, 2014 2:14 pm

Hai Mike,

See post from : by JPB-HK » Sat Dec 29, 2012 2:00 am it has an attachment.
But no further instructions. I have an Oled display that I want to use on my respberry pi (I2C it has 4 connections vcc gnd sda and scl and pops up under adsress 0x3d on my rpi...)

But now how to get it to work... I see many examples but often are build for the arduino or with an Oled with SPI or one with I2C but with 5 wires (RST) and a RST is not available at my oled.

Hence I was hoping this thread here would help me with its examples...

Chrs..
Attachments
oled.png
oled.png (52.17 KiB) Viewed 10907 times

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