Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Arduino Nano with I2c LCD and backlight
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO
View previous topic :: View next topic  
Author Message
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Tue Sep 02, 2014 7:21 pm    Post subject: Arduino Nano with I2c LCD and backlight Reply with quote

Bought a small piece of hardware. It is a I2c module for a LCD-display.



It can be connected to a display like this



And I found in the application note AN #118 a way to display text on this display via I2c.

But got some problems with the backlight.

Made a small addition to the library of Kent, renamed lcd_i2c.lib into bl_lcd_i2c.lib

And with this code backlight can be put on or off.

Code:
'I2c LCD on Arduino Nano 16x2 display with backlight

$regfile = "M328pdef.dat"                                   ' the used chip
$crystal = 16000000                                         ' frequency used
$baud = 9600                                                ' baud rate

Declare Sub I2c_scan

Config I2cdelay = 10

$lib "i2c_twi.lib"                                          'hardware i2c/TWI
Config Twi = 100000

$lib "bl_Lcd_i2c.lib"                                       ' AN #118 library from Kent
                                                            ' with this addition
                                                            ' * lds r27,{backlight}
                                                            ' andi _temp2,&hf7
                                                            ' or _temp2,r27


'LCD-display including backlight
'0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
'1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

'G 5 C R R E D D D D D D D D B B
'N   O S W   0 1 2 3 4 5 6 7 L L
'D V N                         S

'PCF8574
'P7 = D7
'P6 = D6
'P5 = D5
'P4 = D4
'P3 = 1 = backlight on / 0 = backlight off
'P2 = E
'P1 = RW
'P0 = RS

Const Pcf_d4 = 4
Const Pcf_d5 = 5
Const Pcf_d6 = 6
Const Pcf_d7 = 7
Const Pcf_rs = 0
Const Pcf_rw = 1
Const Pcf_e1 = 2

Dim B As Byte
Dim A As Byte
Dim _lcd_e As Byte
Dim Backlight As Byte

Backlight_on Alias &H08
Backlight_off Alias &H00

Backlight = Backlight_on

_lcd_e = 128

Const Pcf8574_lcd = &H4E                                    'Defines the address of the I/O expander for LCD

Config Scl = Portc.5                                        'we need to provide the SCL pin name
Config Sda = Portc.4
I2cinit

'Call I2c_scan                                              'if you want to check if the PCF8574 is seen activate this

Wait 2


cls
home
Lcd "i2c-display"                                           'display this at the top line
Wait 1
Lowerline                                                   'select the lower line
Wait 1
Lcd "Shift this."                                           'display this at the lower line
Wait 1
For A = 1 To 10
   Shiftlcd Right                                           'shift the text to the right
   Waitms 250                                               'wait a moment
Next

For A = 1 To 10
   Shiftlcd Left                                            'shift the text to the left
   Waitms 250                                               'wait a moment
Next


Locate 2 , 1                                                'set cursor position
Lcd "*"                                                     'display this
Wait 1                                                      'wait a moment

Shiftcursor Right                                           'shift the cursor
Lcd "@"                                                     'display this
Wait 1                                                      'wait a moment

Home Upper                                                  'select line 1 and return home
Lcd "Replaced."                                             'replace the text
Wait 1                                                      'wait a moment

Cursor Off Noblink                                          'hide cursor
Wait 1                                                      'wait a moment
Cursor On Blink                                             'show cursor
Wait 1                                                      'wait a moment
Display Off                                                 'turn display off
Wait 1                                                      'wait a moment
Display On                                                  'turn display on

'Now lets build a special character
'the first number is the characternumber (0-7)
'The other numbers are the rowvalues
'Use the LCD tool to insert this line
Deflcdchar 2 , 32 , 10 , 32 , 14 , 17 , 17 , 17 , 14        ' replace ? with number (0-7)
Deflcdchar 0 , 32 , 4 , 32 , 14 , 18 , 18 , 18 , 13         ' replace ? with number (0-7)
Deflcdchar 1 , 32 , 10 , 32 , 14 , 18 , 18 , 18 , 13        ' replace ? with number (0-7)
_lcd_e = 128
Cls                                                         'select data RAM
Rem it is important that a CLS is following the deflcdchar statements because it will set the controller back in datamode
Lcd Chr(0) ; Chr(1)                                         'print the special character

'----------------- Now use an internal routine ------------
_temp1 = 2                                                  'value into ACC
!rCall _write_lcd                                           'put it on LCD

'**************************************************************************

End

Sub I2c_scan                                                ' check all devices on the I2c-bus
Print "Scan start"
For B = 0 To 254 Step 2                                     'for all odd addresses
  I2cstart                                                  'send start
  I2cwbyte B                                                'send address
  If Err = 0 Then                                           'we got an ack
     Print "Slave at : " ; B ; " hex : " ; Hex(b) ; " bin : " ; Bin(b)
  End If
  I2cstop                                                   'free bus
Next
Print "End Scan"
End Sub
 


You can use this code to scan the I2c bus to find your I2c_LCD module. I have found it on address &H4E
On the Arduino Nano I have used the hardware I2c
In the code you will find the small addition I have made to get the backlight working. I found this small piece of code in the German Bascom-forum.

And here the module, a LCD working on a breadboard.



Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5917
Location: Holland

blank.gif
PostPosted: Tue Sep 02, 2014 10:06 pm    Post subject: Reply with quote

Thanks for sharing Ben, well done.
Amazing what you can buy. A good solution when having not enough pins for an LCD.

_________________
Mark
Back to top
View user's profile Visit poster's website
sasha_1973

Bascom Member



Joined: 30 Jul 2013
Posts: 58

ukraine.gif
PostPosted: Mon Mar 02, 2015 2:27 am    Post subject: Reply with quote

Good health!

Does not work?
Can tell me, what's the problem?
Back to top
View user's profile
maxneo

Bascom Member



Joined: 07 Sep 2010
Posts: 35

PostPosted: Sat Jul 04, 2015 1:56 pm    Post subject: 4x20 lcd Reply with quote

This lib does not work for 4x20 display is the one that has the lib for it?
Sincerely
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Thu Aug 13, 2015 5:24 pm    Post subject: Reply with quote

Strange, it works for me...



with this code

Code:

'I2c LCD 4 x 20 on a Arduino Mega

'programmer Arduino Stk500v2, com54, 115200 baud

$regfile = "M2560def.dat"                                   ' the used chip
$crystal = 16000000                                         ' frequency used
$baud = 9600                                                ' baud rate

Declare Sub I2c_scan

Config I2cdelay = 10

$lib "bl_Lcd_i2c.lib"                                       ' AN #118 library from Kent
                                                            ' with this addition
                                                            ' * lds r27,{backlight}
                                                            ' andi _temp2,&hf7
                                                            ' or _temp2,r27


'LCD-display including backlight
'0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
'1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

'G 5 C R R E D D D D D D D D B B
'N   O S W   0 1 2 3 4 5 6 7 L L
'D V N                         S

'PCF8574
'P7 = D7
'P6 = D6
'P5 = D5
'P4 = D4
'P3 = 1 = backlight on / 0 = backlight off
'P2 = E
'P1 = RW
'P0 = RS

Const Pcf_d4 = 4
Const Pcf_d5 = 5
Const Pcf_d6 = 6
Const Pcf_d7 = 7
Const Pcf_rs = 0
Const Pcf_rw = 1
Const Pcf_e1 = 2


Dim B As Byte
Dim A As Byte
Dim _lcd_e As Byte
Dim Backlight As Byte

Backlight_on Alias &H08
Backlight_off Alias &H00

Backlight = Backlight_on

_lcd_e = 128

Const Pcf8574_lcd = &H4E                                    'Defines the address of the I/O expander for LCD

Config Scl = Portd.0                                        'we need to provide the SCL pin name
Config Sda = Portd.1
I2cinit

'Call I2c_scan                                              'if you want to check if the PCF8574 is seen activate this

Wait 2


Cls
Home Upper
Lcd "Working bl_lcd.lib"                                    'display this at the top line
Wait 1
Lowerline                                                   'select the lower line
Wait 1
Lcd "on 4x20 lcd without"                                   'display this at the lower line
Wait 1
Thirdline
Lcd "modifications..."
Fourthline
Lcd "Have fun!!"

End


Sub I2c_scan                                                ' check all devices on the I2c-bus
Print "Scan start"
For B = 0 To 254 Step 2                                     'for all odd addresses
  I2cstart                                                  'send start
  I2cwbyte B                                                'send address
  If Err = 0 Then                                           'we got an ack
     Print "Slave at : " ; B ; " hex : " ; Hex(b) ; " bin : " ; Bin(b)
  End If
  I2cstop                                                   'free bus
Next
Print "End Scan"
End Sub


With the standard Bascom lcd-commands....

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Yenary

Bascom Member



Joined: 19 May 2009
Posts: 3
Location: Yogyakarta

indonesia.gif
PostPosted: Tue Sep 29, 2015 10:00 am    Post subject: Re: Arduino Nano with I2c LCD and backlight Reply with quote

bzijlstra wrote:
Bought a small piece of hardware. It is a I2c module for a LCD-display.



It can be connected to a display like this



And I found in the application note AN #118 a way to display text on this display via I2c.

But got some problems with the backlight.

Made a small addition to the library of Kent, renamed lcd_i2c.lib into bl_lcd_i2c.lib

And with this code backlight can be put on or off.

Code:
'I2c LCD on Arduino Nano 16x2 display with backlight

$regfile = "M328pdef.dat"                                   ' the used chip
$crystal = 16000000                                         ' frequency used
$baud = 9600                                                ' baud rate

Declare Sub I2c_scan

Config I2cdelay = 10

$lib "i2c_twi.lib"                                          'hardware i2c/TWI
Config Twi = 100000

$lib "bl_Lcd_i2c.lib"                                       ' AN #118 library from Kent
                                                            ' with this addition
                                                            ' * lds r27,{backlight}
                                                            ' andi _temp2,&hf7
                                                            ' or _temp2,r27


'LCD-display including backlight
'0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
'1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

'G 5 C R R E D D D D D D D D B B
'N   O S W   0 1 2 3 4 5 6 7 L L
'D V N                         S

'PCF8574
'P7 = D7
'P6 = D6
'P5 = D5
'P4 = D4
'P3 = 1 = backlight on / 0 = backlight off
'P2 = E
'P1 = RW
'P0 = RS

Const Pcf_d4 = 4
Const Pcf_d5 = 5
Const Pcf_d6 = 6
Const Pcf_d7 = 7
Const Pcf_rs = 0
Const Pcf_rw = 1
Const Pcf_e1 = 2

Dim B As Byte
Dim A As Byte
Dim _lcd_e As Byte
Dim Backlight As Byte

Backlight_on Alias &H08
Backlight_off Alias &H00

Backlight = Backlight_on

_lcd_e = 128

Const Pcf8574_lcd = &H4E                                    'Defines the address of the I/O expander for LCD

Config Scl = Portc.5                                        'we need to provide the SCL pin name
Config Sda = Portc.4
I2cinit

'Call I2c_scan                                              'if you want to check if the PCF8574 is seen activate this

Wait 2


cls
home
Lcd "i2c-display"                                           'display this at the top line
Wait 1
Lowerline                                                   'select the lower line
Wait 1
Lcd "Shift this."                                           'display this at the lower line
Wait 1
For A = 1 To 10
   Shiftlcd Right                                           'shift the text to the right
   Waitms 250                                               'wait a moment
Next

For A = 1 To 10
   Shiftlcd Left                                            'shift the text to the left
   Waitms 250                                               'wait a moment
Next


Locate 2 , 1                                                'set cursor position
Lcd "*"                                                     'display this
Wait 1                                                      'wait a moment

Shiftcursor Right                                           'shift the cursor
Lcd "@"                                                     'display this
Wait 1                                                      'wait a moment

Home Upper                                                  'select line 1 and return home
Lcd "Replaced."                                             'replace the text
Wait 1                                                      'wait a moment

Cursor Off Noblink                                          'hide cursor
Wait 1                                                      'wait a moment
Cursor On Blink                                             'show cursor
Wait 1                                                      'wait a moment
Display Off                                                 'turn display off
Wait 1                                                      'wait a moment
Display On                                                  'turn display on

'Now lets build a special character
'the first number is the characternumber (0-7)
'The other numbers are the rowvalues
'Use the LCD tool to insert this line
Deflcdchar 2 , 32 , 10 , 32 , 14 , 17 , 17 , 17 , 14        ' replace ? with number (0-7)
Deflcdchar 0 , 32 , 4 , 32 , 14 , 18 , 18 , 18 , 13         ' replace ? with number (0-7)
Deflcdchar 1 , 32 , 10 , 32 , 14 , 18 , 18 , 18 , 13        ' replace ? with number (0-7)
_lcd_e = 128
Cls                                                         'select data RAM
Rem it is important that a CLS is following the deflcdchar statements because it will set the controller back in datamode
Lcd Chr(0) ; Chr(1)                                         'print the special character

'----------------- Now use an internal routine ------------
_temp1 = 2                                                  'value into ACC
!rCall _write_lcd                                           'put it on LCD

'**************************************************************************

End

Sub I2c_scan                                                ' check all devices on the I2c-bus
Print "Scan start"
For B = 0 To 254 Step 2                                     'for all odd addresses
  I2cstart                                                  'send start
  I2cwbyte B                                                'send address
  If Err = 0 Then                                           'we got an ack
     Print "Slave at : " ; B ; " hex : " ; Hex(b) ; " bin : " ; Bin(b)
  End If
  I2cstop                                                   'free bus
Next
Print "End Scan"
End Sub
 


You can use this code to scan the I2c bus to find your I2c_LCD module. I have found it on address &H4E
On the Arduino Nano I have used the hardware I2c
In the code you will find the small addition I have made to get the backlight working. I found this small piece of code in the German Bascom-forum.

And here the module, a LCD working on a breadboard.



Have fun
Ben Zijlstra



Thank very much. Very helpfull. my problem solved.

_________________
Stephen Mahago
Back to top
View user's profile Yahoo Messenger MSN Messenger
yamaja

Bascom Member



Joined: 27 Sep 2015
Posts: 3

blank.gif
PostPosted: Sun Oct 18, 2015 8:33 am    Post subject: successfully first device, but trouble in the second devices Reply with quote

Hi Ben. nice to meet you. really thanks for your awesome sharing.

by the way, we have successfully run the text using i2c lcd 16x2, an atmega 16

but when we try to use for the second devices, it seem something troubling us. the lcd did not show any text, only a blinking cursor. keep running. the micro can communicate to the lcd to close down the light.


we have try 3 (three) different module, the result is same, it can not show the text.
we have attach the video to show the result.

could you tell us how to fix this problem ?

Thanks ben,


--------------------------------------------------
after several days troubleshooting, we found that the problem lies in the lcd. we have bought 10 lcd, only 1 work normally:cry: , . after using other brand of 16 x 2 lcd", our problem solved. temporary our solution is changes the lcd. i wonder if there is any other solution.

we will share our lcd 16 x 2 picture that do not work, hope other member that met this problem can be useful. thanks for this great forum Smile
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Thu Oct 29, 2015 12:15 pm    Post subject: delays and pull-ups... Reply with quote

I must have missed your question, now I have seen you got some trouble with the adapted library.

Perhaps you have got a timing problem. Perhaps you can go over the library and add some NOP's, specially when writing commands en data to the display.

And you could check if you got pull-ups on your I2c-adapter. There should be 10 K ohm resistors to VCC on both SDA and SCL.

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
yamaja

Bascom Member



Joined: 27 Sep 2015
Posts: 3

blank.gif
PostPosted: Fri Oct 30, 2015 5:36 am    Post subject: Reply with quote

thanks ben. you are such a great person Smile
love this forum. Go !!!!
Back to top
View user's profile
digibert

Bascom Member



Joined: 03 Dec 2005
Posts: 1

germany.gif
PostPosted: Thu Mar 10, 2016 5:46 pm    Post subject: Reply with quote

Hi Ben,

i have bought at banggood three LCD 1602-modules with exact the same I2C-Adaptors mounted already.
I have tried now several examples found in internet to drive them with BASCOM, without any success. (my mc also is a "arduino nano")

I found now your solution and tried it, with one step forward but have the same effect as "Yamaja":
I can swich the backlight On/Off without problem, but when sending text to LCD i get only one blinking cursur for a moment.
(means my I2C-Adress found with hex7E is ok and the pin-layout must be the same as yours)

i do not understand how to modify your lib with nops, may i ask to tell me more please ?
(my pull-up resistors on SDA and SCL are ok)
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Wed Jun 15, 2016 1:40 pm    Post subject: Reply with quote

Ben
I have been having trouble with this sometimes it worked then not.
Well I found it if I do not start with "home" I get problems

Regards Paul
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Wed Jun 15, 2016 2:27 pm    Post subject: Home sweet home... Reply with quote

Paul

Thanks for your reply. So home should be used.

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Thu Jun 16, 2016 9:59 am    Post subject: Reply with quote

Here is a short program that when the "Home" is removed puts random text on LCD


Regards Paul

Code:




$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 57600
$hwstack = 100                                              ' default use 32 for the hardware stack
$swstack = 100                                              'default use 10 for the SW stack
$framesize = 100                                            'default use 40 for the frame space

Config I2cdelay = 40

$lib "i2c_twi.lib"                                          'hardware i2c/TWI
Config Twi = 100000

$lib "bl_Lcd_i2c.lib"                                       ' AN #118 library from Kent


Backlight_on Alias &H08
Backlight_off Alias &H00

Const Pcf_d4 = 4
Const Pcf_d5 = 5
Const Pcf_d6 = 6
Const Pcf_d7 = 7
Const Pcf_rs = 0
Const Pcf_rw = 1
Const Pcf_e1 = 2


Dim B As Byte
Dim A As Byte
Dim _lcd_e As Byte
Dim Backlight As Byte



_lcd_e = 128

Const Pcf8574_lcd = &H4E                                    'Defines the address of the I/O expander for LCD




Config Scl = Portc.5                                        'we need to provide the SCL pin name
Config Sda = Portc.4

'=======================================
  'without this you get rubbish on LCD at power up
    Home
'=======================================
   Locate 1 , 1
   Lcd "Test Line 1"


   Locate 2 , 1
   Lcd "Test Line 2"





   End
 
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1257
Location: SYDNEY

australia.gif
PostPosted: Fri Jun 17, 2016 12:38 pm    Post subject: Reply with quote

Looking at this library I thought why is the address a constant
why is it not a variable
For my first stab at ASM
so I made it one
and it worked!!
But I only have one I2C Lcd module
so I changed the address of the module with the jumper on the back to &H46
Then dimension a variable Pcf8574_loc as byte
then Pcf8574_loc = &H46
So why do this?
We can now have multiple LCDs it is a bus after all !

Ben if you have more than one could you test it!

Regards Paul
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Fri Jun 17, 2016 2:05 pm    Post subject: Will do a test with several I2c LCDs Reply with quote

Thanks Paul

Will do a test with more than one I2c LCD.
And publish the result

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-ARDUINO All times are GMT + 1 Hour
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum