Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Help on I2C


  • Please log in to reply
55 replies to this topic

#1 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 15 March 2014 - 10:28 AM

Hi

 

i have a LCD display with I2C.

I tried for a few days now, but total no communication.

Is there a piece of sample code to test I2c ?  Maybe ask the device adresses on the bus or something

 

thanks Frank

 



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 15 March 2014 - 01:11 PM

Hi FBeerens,

What ohm value are you using for the pull-up resistors? Do you have the display's I2C bus plugged into the SDA/SCL pins, or into pins A4/A5?

Chris

#3 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 15 March 2014 - 08:42 PM

       OutputPort op = new OutputPort(Pins.GPIO_PIN_SDA, true);
            bool noton=false;

            while (true)
            {
                noton=!noton;
                op.Write(noton);
                Thread.Sleep(200);
            }

Hi Chris

 

i have a netduino 2 plus connected to SDA/SCL and one netduino 2 connected to A4/A5

The resistors are 4k7.

I another thread you where talking about testing with a led. I tried that also but maybe i did something but the led never turns on.

 

Pin 4 -> resistor 150 Ohm -> led -> gnd  , i already turned the led from positive to negative but no result.

 

this is the code



#4 sfugarino

sfugarino

    Member

  • Members
  • PipPip
  • 29 posts
  • LocationSuwanee, GA

Posted 21 March 2014 - 06:05 AM

Try using the I2CBus class. It abstracts away most of the work of talking to an I2C device:

http://wiki.netduino....ashx?HL=i2cbus

At the top of your main toggle the SDA output

OutputPort op = new OutputPort(Pins.GPIO_PIN_SDA, false);
op.Write(true);
Thread.Sleep(200);
op.Write(false);

#5 Frode

Frode

    Advanced Member

  • Members
  • PipPipPip
  • 202 posts
  • LocationNorway

Posted 21 March 2014 - 01:43 PM

An alternative to the initial SDA toggling can be changing the Write-method in the I2CBus as described here.



#6 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 21 March 2014 - 05:46 PM

thanks all for your answers.

 

i think i have to start from the beginning.

How do i connect the bus ? Which resistors do i need ? and can give someone me some example code to test if a can connect / communicatie with a slave ?



#7 mbrossett

mbrossett

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 25 March 2014 - 01:45 AM

What is the part number of the LCD you are using. This may help us help you. If it is a Newhaven Display serial LCD then you need to install an on-board jumper to configure it in I2C mode. Your fix probably isn't that easy but just thought I'd throw that out there. Also, if you give me the part number I could write you a simple piece of test code.

#8 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 25 March 2014 - 07:40 AM

It is a LiquidCrystal_I2C

 

Can you also help me with the resistors and the connections



#9 mbrossett

mbrossett

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 25 March 2014 - 01:51 PM

I cannot find that display. Can you send me a link to the website you bought it from?



#10 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 25 March 2014 - 02:00 PM

Hi it is a dutch site

 

https://www.iprototy...cts/components/led-lcd/lcd16x2-I2C-BL

 

there is a library for arduino on that site.



#11 mbrossett

mbrossett

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 25 March 2014 - 02:52 PM

Ok, I looked at that display for a bit and it uses a parallel type display with a I2C gpio expander. Also, it is a 5V display so you would need to ensure that you are powering it from 5V and that the I2C pull-ups are to 5V. If you are new to METMF and Netduino I would suggest going with a true I2C serial display such as one of these...

 

http://www.newhavend...lays-c-253.html

 

Although the Arduino library can be ported to NETMF, you will be better off going with one of the displays I have pointed you to. With NETMF not being a real-time environment you might run into issues with the timing on this display anyway, especially in a multi-threaded application, but I'm not for sure. Another option is to use an Arduino instead of a Netduino. Sorry I couldn't be more helpful, maybe someone with a little more free time and ambition will come by and port it over for you.



#12 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 25 March 2014 - 06:08 PM

oke thanks, i will order one of those displays. But do you have the source code for me for these displays ?

 

Which resistors should i use and which voltage for these?



#13 mbrossett

mbrossett

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 25 March 2014 - 08:29 PM

These are 5V displays, too. Therefore, you will need 5V pull-ups on the I2C lines. When you get the display you will need to solder a 0 Ohm resistor or something (piece of wire or bridge it with solder like I do) across R1, see page 4 of the datasheet...

 

http://www.newhavend...Z-FL-GBW-V3.pdf

 

And yes I will post some code tonight for this display. I have a driver for it on my PC at home. Will post it tonight.



#14 mbrossett

mbrossett

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 25 March 2014 - 08:32 PM

Btw, there are pull-up resistors (10k) installed on the LCD so you shouldn't need them, but you may want to put a little stronger pull-ups on the bus (4.7k, 3.3k, or 2.2k) just in case.



#15 mbrossett

mbrossett

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 25 March 2014 - 11:56 PM

As promised here is your driver and test code for the Newhaven Display I2C serial displays. Enjoy and keep us posted on the progress.

using System;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace NewhavenDisplay
{
    // For use with newhaven I2C displays
    // I2C protocol:
    // To enter the I2C mode, place a jumper on R1.  
    // SDA and SDK have pull?up resistors (10K Ohm) on R7 and R8.
    // The default I2C address is 80 (50 hex).  The I2C address can be changed to any 8?bit value by command function, with
    // the exception that the LSB (least significant bit) must always be 0.  Once the I2C address has been changed, it will be
    // saved in the system memory, and it will revert back to the default address if either RS232 or SPI protocol is selected.
    // The I2C interface is capable of receiving data at up to 100KHz?clock rate.
    // See http://www.newhavendisplay.com/specs/NHD-0216K3Z-FL-GBW.pdf

    public class SerialLCD
    {
        private static I2CDevice.Configuration _config = new I2CDevice.Configuration(0x50 >> 1, 100); 

        public SerialLCD()
        {
        }

        public void Write(I2CDevice device, string sText)
        {
            device.Config = _config;
            var xActions = new I2CDevice.I2CTransaction[1];
            byte[] buf = Encoding.UTF8.GetBytes(sText);                 // Convert the string to array
            xActions[0] = I2CDevice.CreateWriteTransaction(buf);
            device.Execute(xActions, 1000);
        }

        public void DisplayOn(I2CDevice device)
        {
            device.Config = _config;
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xFE, 0x41 });
            device.Execute(xActions, 1000);
        }

        public void DisplayOff(I2CDevice device)
        {
            device.Config = _config;
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xFE, 0x42 });
            device.Execute(xActions, 1000);
        }

        public void SetCursor(I2CDevice device, byte Position)
        {   // 0x00 = line 1 column 1
            // 0x40 = line 2 column 1
            device.Config = _config;
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xFE, 0x45, Position });
            device.Execute(xActions, 1000);
        }

        public void Clear(I2CDevice device)
        {
            device.Config = _config;
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xFE, 0x51 });
            device.Execute(xActions, 1000);
        }

        public void SetContrast(I2CDevice device, byte ContrastLevel)
        {   // Value between 0 and 50, default is 40
            device.Config = _config;
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xFE, 0x52, ContrastLevel });
            device.Execute(xActions, 1000);
        }

        public void SetBrightness(I2CDevice device, byte BrightnessLevel)
        {   // Value between 1 to 8 
            device.Config = _config;
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xFE, 0x53, BrightnessLevel });
            device.Execute(xActions, 1000);
        }

        public void Firmware(I2CDevice device)
        {
            device.Config = _config;
            var xActions = new I2CDevice.I2CTransaction[1];
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xFE, 0x70 });
            device.Execute(xActions, 1000);
        }

    }
}
using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using NewhavenDisplay;

namespace Serial_Display_Test
{
    public class Program
    {
        public static void Main()
        {
            // Create an I2C device with no device configuration
            I2CDevice i2cDevice = new I2CDevice(null);

            SerialLCD display = new SerialLCD();

            display.DisplayOn(i2cDevice);

            // Write to display
            display.Clear(i2cDevice);
            display.SetCursor(i2cDevice, 0x00);
            display.Write(i2cDevice, "Hello");

            // Wait indefinitely
            Thread.Sleep(Timeout.Infinite);
        }
    }
}


#16 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 31 March 2014 - 05:51 AM

ok thanks, finally it arrived.

 

i ordered the NHD-420D3Z-FL-GBW-V3.

 

is het hole with the square pin 1?



#17 mbrossett

mbrossett

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 01 April 2014 - 05:34 PM

Yes, pin 1 of each connector is marked with a square solder pad, as opposed to the round pad.



#18 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 07 April 2014 - 07:06 PM

finally found someone to solder for me.

Now i connected the power supply from the netduino but the lcd does not turn on ?

Is the power of the netduinoe not enough for the lcd ?



#19 Frank Beerens

Frank Beerens

    Advanced Member

  • Members
  • PipPipPip
  • 33 posts
  • LocationNetherlands

Posted 07 April 2014 - 07:20 PM

oke the square does nog seem to be pin 1. but the oppesite pin seams to be pin 1.

When i connect the power supply the lcd lights up.

 

i connected SDA and SCL to the SDA en SCL connectors on the netduino 2.

The code is running but i do net get hello on the lcd ? Wat can i test to get it up and running ?



#20 mbrossett

mbrossett

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 07 April 2014 - 08:48 PM

Here's a few things to check...

 

1) Is there a jumper / short across R1? (R2 should be left open)

2) Measure the voltages of SDA and SCL while the bus is idle...are they pulled up to 5V?

3) Try adding a delay at the beginning of your code to ensure LCD is up and configure ("Thread.Sleep(1000);")

4) Step through the code using the debugger and ensure the "device.Execute(...);" functions are returning a non-zero value. You will need to change the code slightly... "int ret = device.Execute(...);"

5) Try setting the brightness and contrast..."display.SetBrightness(0x07);" ... "display.SetContrast(0x30);"

 

The Netduino should be capable of providing the 250mA to the display, but you can verify this by measuring the voltage at the display when it is plugged in. If the voltage is above 4.7V it is fine.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.