Author Topic: == Arduino I2C Source Code ==  (Read 4788 times)

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
== Arduino I2C Source Code ==
« on: May 29, 2014, 05:48:08 PM »
  • See first post below (created by Matt) for connecting to Arduino UNO and sending "Hello World" message.
  • See second post for Turning the backlight on and off, and changing it's brightness
  • See third post for Turning the display on and off, and changing it's contrast
« Last Edit: June 16, 2014, 07:09:58 PM by modtro2 »

Matt

  • Newbie
  • *
  • Posts: 1
    • View Profile
LCD2S via I2C with Arduino tutorial, for KNOWN I2C address
« Reply #1 on: June 09, 2014, 07:53:16 PM »
The Modtronix LCD2S family of Serial LCD Displays can be configured to communicate via the I2C or SPI (mode 0) protocol. To use the I2C mode, both switches on the DIP switch must be set to the off (0) position. This will configure the LCD2S for the I2C address 0x50, which is 0x28 for Arduino code (see "Standard I2C to Arduino I2C address conversion" section in troubleshooting below). Please refer to the LCD2S data sheet (link: http://modtronix.com/shop/lcd2s-204yhy.html) to find the possible DIP switches combination for different I2C addresses.

Step 1:
Connect the LCD2S module with an Arduino. In this case the I2C bus pin on an Adruino UNO is as follow: (Refer to Adruino page for other boards' I2C pins)
===== For I2C operation =====
- LCD2S SDI to Uno SDA (A4)
- LCD2S CLK to Uno SCL (A5)
- LCD2S 5V to Uno 5V
- LCD2S 0V to Uno Gnd


Fire up the the LCD!!!


Step 2:
Write your own sketch:
-Make sure both DIP switches are in the correct position


-Open a new sketch
-Copy and paste the following skeleton code for writing "Hello World" into your new sketch:
Code: [Select]

#include <Wire.h>

void setup()
{
  Wire.begin();  //Join bus as master
 
  Wire.beginTransmission(0x28);  //Start condition at the given I2C address or Mark the beginning of a data packet, change 0x28 to your device's I2C address
 
  Wire.write(0x80);  //Initiate Display Commands
  Wire.write(0x0c);  //Clear display and go to first line
 
  Wire.write("Hello World!");  //Write message
 
  Wire.endTransmission(false);  //End condition or Mark the end of data packet and send to LCD and hold bus connection
}

void loop()
{
}
-Change the "0x28" address in the skeleton code with your device's I2C address.
-Upload and enjoy.


-To begin using the LCD2S, there are two pages you need to visit and learn
1. For further LCD commands, refer to LCD2S data sheet which can be found here: http://modtronix.com/shop/lcd2s-204yhy.html
2. To enable Arduino to communicate via I2C, you need to use Wire library. For I2C commands for Arduino, visit: http://arduino.cc/en/reference/wire

Additional Features:
The LCD2S has many additional features, which are available in the LCD2S data sheet. Some features are:
  • LCD Backlighting controlled via software, has 254 brightness levels
  • LCD Contrast controlled via software, has 254 contrast levels
  • Built in commands for drawing Bar Graphs
  • User configurable start up screen
  • Two high current, open collector outputs with output currents up to 1000mA each, and relay protection circuitry
  • 3 general purpose input/output pins
  • Keypad encoder for a keypad up to 16 keys (4 rows by 4 columns)
  • Can be configured to sound a buzzer (connected to OUT2) each time a button is pressed

Troubleshooting:
  • Standard I2C to Arduino I2C address conversion: When using Arduino Wire functions, the standard I2C address has to be shifted 1 bit right to get the address required for wire functions. For example, the standard LCD2S address is 0x50, which is 0101 000. When shifted 1 bit right, it becomes 0010 1000, which is hex 0x28.
  • If it doesn't work for you, it might be the case that your I2C address is wrong! This can happen if the "Change I2C Base Address" command has been sent to the LCD2S device to change it's base address. If this is the case, see this post for details on how to find the I2C address of a device with unknown I2C address.

Additional Photos:
See attachements
« Last Edit: June 16, 2014, 06:17:00 PM by modtro2 »

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: == Arduino I2C Source Code ==
« Reply #2 on: June 16, 2014, 06:58:09 PM »
Turning the backlight on and off, and changing it's brightness:
The following Arduino examples show how to turn the backlight on and off, and change the brighness. The brightness can be set to 254 levels. See the LCD2S datasheet available on this page for details!

Code: [Select]

#include <Wire.h>

void setup()
{
  //Turn backlight off. Send "Backlight Off" command to LCD2S with default address 0x28 (I2C 0x50)
  Wire.begin();
  Wire.beginTransmission(0x28);
  Wire.write(0x20);  //"Backlight Off" command
  Wire.endTransmission(false);

  //Turn backlight on. Send "Backlight On" command to LCD2S with default address 0x28 (I2C 0x50)
  Wire.begin();
  Wire.beginTransmission(0x28);
  Wire.write(0x28);  //"Backlight On" command
  Wire.endTransmission(false);

  //Set backlight brightness. Send "Set Backlight Brightness" command to LCD2S with default address 0x28 (I2C 0x50)
  Wire.begin();
  Wire.beginTransmission(0x28);
  Wire.write(0x81);  //Send "Set Backlight Brightness" command.
  Wire.write(150);  //Parameter for "Set Backlight Brightness" command. 0 will be off, and 254 will be maximum brightness.
  Wire.endTransmission(false);
}

void loop()
{
}

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: == Arduino I2C Source Code ==
« Reply #3 on: June 16, 2014, 07:04:47 PM »
Turning the display on and off, and changing it's contrast:
The following Arduino examples show how to turn the display on and off, and change the contrast. The contrast can be set to 254 levels. See the LCD2S datasheet available on this page for details!

Code: [Select]

#include <Wire.h>

void setup()
{
  //Turn display off. Send "display Off" command to LCD2S with default address 0x28 (I2C 0x50)
  Wire.begin();
  Wire.beginTransmission(0x28);
  Wire.write(0x12);  //"display Off" command
  Wire.endTransmission(false);

  //Turn display on. Send "display On" command to LCD2S with default address 0x28 (I2C 0x50)
  Wire.begin();
  Wire.beginTransmission(0x28);
  Wire.write(0x1a);  //"display On" command
  Wire.endTransmission(false);

  //Set display contrast. Send "Set display Contrast" command to LCD2S with default address 0x28 (I2C 0x50)
  Wire.begin();
  Wire.beginTransmission(0x28);
  Wire.write(0x82);  //Send "Set display Contrast" command.
  //Parameter for "Set Display Contrast" command. 0 will be off, and 254 will be maximum contrast.
  //Normally a value between 190 and 225 gives the best results.
  Wire.write(200);
  Wire.endTransmission(false);
}

void loop()
{
}

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: == Arduino I2C Source Code ==
« Reply #4 on: June 16, 2014, 07:12:41 PM »
Additional Features:
For additional features, see other posts in this forum.