robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 8:12 am

Hi all,

Using Python, I have created a small thermostat control system, whereby I use a Slice of Pi/o V1.2 (http://openmicros.org/GSG/Slice%20of%20 ... 0guide.pdf) to achieve the following functions:

- Drive a multiplexed 4 digit 7 segment display (using 11 outputs - 7 for the segments, and 4 outputs to drive a transistor for each of the common cathodes) to show current and target temperatures.
- Two press buttons, used for changing the target temperature.
- One buzzer output, to create and audio alert when the target temperature is reached.
- Two outputs connected to relays to power two heaters.
- I also have a couple of 3 wire thermometers connected to JP2 on the Slice of Pi/o (GPIO 4) as per this website (http://www.raspberrypi-spy.co.uk/2013/0 ... er-sensor/)

It all works very well, in basic control terms (hardly rocket science). However, the multiplexing of the 7 segments is very processor intensive (for such a silly little task) and is prone to horrendous flickering and blurring of numbers, which makes it look pretty rubbish.

I was wondering how easy it might be to remove the 7 segment display, and replace it with a cheap HD44780 compatible LCD like this one (http://www.ebay.co.uk/bhp/raspberry-pi-lcd).

I have read a number of online guides to see if I can connect it in 4 bit mode (http://www.raspberrypi-spy.co.uk/2012/0 ... ng-python/). They all seem to use GPIOs 18, 23, 24 & 25, but my Slice of Pi/o only allows the use of GPIOs 0-7 from JP2 (and I am already using GPIO 4 for the 3 wire thermometer).

The link I provided for the LCD suggests that I can assign different available GPIOs to drive the LCD but I have no idea if I will encounter any problems with operating the Slice of Pi/o and the the LCD at the same time (I would need to use something like GPIOs 0, 1, 2, & 3 for the data lines, and GPIO 5 for register select and GPIO 6 for enable/strobe)? Will this work, and will I need to do anything special with the Slice of Pi/o connected?

Thanks

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 8:23 am

Actually, looking at the annotation on the Slice of Pi/o, the JP2 seems to show annotation for GPIOs 4, 17, 18, 22, 23, 24, 25, & 27 (whereas the datasheet on the second page of the PDF lists GPIO 0-7).

So, it would seem that the correct GPIOs are available for the LCD.

However, I would still be interested in knowing if there are any issues with operating the Slice of Pi/o and the LCD at the same time?

Thanks again

AJB2K3
Posts: 215
Joined: Sun Aug 11, 2013 3:25 pm

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 5:30 pm

Did you read the bit in the instructions that states that the PI/o's can be chained together for more i/o's?

Alternatively look into an i2c lcd?

Just a thought.
Back in the old days, we were smarter then the technology.
Nowadays technology makes us look like villages fools in front of the children!

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 5:40 pm

Hi AJB2K3

I did know that I could do that. Are you proposing that I do this to drive the 7 segment displays without multiplexing (to avoid the refresh problems), or is this something linked to how the LCD might be use?

Sorry, I am not experienced enough to understand what you are proposing :oops:

AJB2K3
Posts: 215
Joined: Sun Aug 11, 2013 3:25 pm

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 6:27 pm

robbo100 wrote:Hi AJB2K3

I did know that I could do that. Are you proposing that I do this to drive the 7 segment displays without multiplexing (to avoid the refresh problems), or is this something linked to how the LCD might be use?

Sorry, I am not experienced enough to understand what you are proposing :oops:
No, I must apologise as I tend to be confusing. The Slice of Pi/o makes use of the i2c bus and if more gpio's are needed then you can add more slice of pi/os via the sda/sdl lines. how ever you may run into problems with power supplies.

As long as the pins you would like to use are available and you set the correct pins in code then in theory everything hould be fine.

As to how to do this look in the display forum, I can't help you as I run a usb lcd via LCDd & LCDproc.
Back in the old days, we were smarter then the technology.
Nowadays technology makes us look like villages fools in front of the children!

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 6:30 pm

Coupla ways to go. Find a 16x2 display that can be used over I2C (as AJB2K3 suggested). That means it will include a PCF8574 expander or MCP23017 expander that can drive the LCD with I2C programming (code exists for both expander chips). Sainsmart, Adafruit, Sparkfun and undoubtedly EBay have these. Or you could wire up your own expander chip to a "bare" LCD (that uses the Hitachi HD44780).

Or, as suggested here, add a MAX7219 display driver to your current 7-segment display. It can be programmed via the SPI bus, and will off-load the multiplexing from the Pi. That's cheaper than the LCD, but will only drive up to 8 7-segment digits.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 7:28 pm

Adafruit has drivers and tutorial for driving a display with a MCP23008. There is no reason why a MCP23017 could not be used.

See http://learn.adafruit.com/i2c-spi-lcd-backpack

With a max of 8 23017 chips per bus and two LCD displays per chip, you could drive 16 displays on one bus. Use both I2c Busses and that number doubles to 32. Use the SPI bus with MCP23S17 and you could add another 16 displays for a grand total of 48 displays per Rpi.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 7:34 pm

If you really want to go nuts, get a I2C Mux. Add up to sixty I2C buses to the Rpi. (address 0x70 is already used on on I2c bus on the Rpi)
http://dsscircuits.com/i2c-multiplexer.html

Image
http://www.nxp.com/documents/data_sheet ... F8574A.pdf

If you need several chips that have only one i23c address, this is how you can do it.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 7:41 pm

Richard-TX wrote:(address 0x70 is already used on on I2c bus on the Rpi)
Not on my RPi it isn't

Code: Select all

[email protected] ~ # i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
[email protected] ~ #
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Mon Dec 30, 2013 8:14 pm

You are correct but it depends on what is on the bus. In the case of the PCA9685, address 0x70 is used as well as it's normal 0x40 address. This is due to the all-call feature.


Image
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 1:46 pm

Hi all,

Thanks for your replies and help.

If I am honest, I am probably slightly out of my depth :lol:

Am I correct in saying that if my Slice of Pi/o is using the same address as the LCD will use then it won't work?

I have the following address being used:

Code: Select all

[email protected] ~ $ i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 
and the code I was planning on using addresses as follows (assuming I know what I am talking about and not just talking nonsense!)

Code: Select all

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)  
I assume the fact that I am already using 0x40, and the high bits code calls 0x40, this will mean that it won't work???

Sorry, I am a bit of a numpty!

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 1:48 pm

P.S. I bought that cheap display from ebay (since it was only a couple of quid), and hopefully it will arrive tomorrow, so I will be able to try it in any case.

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 4:12 pm

robbo100 wrote:
Am I correct in saying that if my Slice of Pi/o is using the same address as the LCD will use then it won't work?
Look at step 4
http://openmicros.org/index.php/article ... ice-of-pio
you can change the I2C address for your slice of Pi. Although you'll need to de-solder one (or more) of the joints on the bottom of the board. Such a shame they didn't add a £0.49p dip switch and three resistors to allow it to be changed easily rather than hard wired.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 5:01 pm

DougieLawson wrote:Such a shame they didn't add a £0.49p dip switch and three resistors to allow it to be changed easily rather than hard wired.
Good point!

Sorry to be a pain, but I have found the section which explains the addressing system (Page 8 Figure 1.2 of this document: http://ww1.microchip.com/downloads/en/d ... 21952b.pdf) - but I am unsure if I am calculating the address correctly.

I presume that the current grounding of the three pads has given me 0 for A0, A1 and A2. Looking at the control byte, this will give 0100000 (which is 32 or 0x20).

Am I right in saying that if I want to have an address of 34 (0x22) I should then change it to 0100010 by de-soldering the A1 ground tab and soldering the A1 pwr tab?

Have I understood all this correctly?

Thanks

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 5:28 pm

robbo100 wrote:However, I would still be interested in knowing if there are any issues with operating the Slice of Pi/o and the LCD at the same time?
The LCD linked in the OP is not an I2C device and so no issues with I2C addressing there. It can be connected directly to the Pis GPIOs, or better, to the Slice of Pi/O expanded IOs. With the right wiring and code, shouldn't be a problem. I would try and devote its own port on the MCP23017, and use the other port for sensors and buttons and whatnot, but it's not required, it just might make coding simpler. By the way, the code fragment you quoted is for an LCD connected directly to the Pi's GPIOs.

If you're already using all (16) IOs on the Slice of Pi/O, (and don't feel comfortable wiring the LCD to the Pi itself) then you'll need another Slice O'Pi, or just an MCP23017. Then, as stated, there are ways to uniquely address each MCP23017.

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 6:29 pm

Right then, I am completely confused - it doesn't take much :lol:

To recap, I have a Slice of Pi/o, with some free i/os (although I would like to use them all for if I can).

As well as the digital i/o's the Slice of Pi/o has got another header (JP2) which (for want of a better term) "passes-through" the following GPIOs - 17, 18, 22, 23, 24, 25, 27 & 4 (I believe).

I have already sucessfully connected GPIO 4 on JP2 to a 3 wire thermometer, which works fine.

So, my question was, will there be a problem using GPIOs 17, 18, 22, 23, 24 & 25 to connect an LCD in 4 bit mode (as long as the GPIOs are correctly assigned in the python script).

I think the answer I have been provided by Douglas6, is that this will work fine (and that the addressing of the Slice of Pi/o (0x20) has nothing to do with the addressing of the bits on the GPIOs for the LCD). - Is this correct, or do I need to change the Slice of Pi/o address as I proposed to something like 0x22?

I think the additional information regarding the use of additional Slice of Pi/o's (or other devices) and the changing of addressing etc is additional information, which I would could use, if the GPIO LCD connection doesn't work for some reason. - Is this correct?

Sorry, but I am not massively experienced, and the information provided has been very helpful, but I am struggling with the jargon slightly (sorry).

Thanks again.

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 7:24 pm

I spent ages googling all this, and found nothing, but just had another search and found the following page using the image search function within google - http://raspi.tv/2013/using-the-mcp23017 ... rts-part-3

So, assuming the Slice of Pi/o V1.2 is wired up in the same way as this site shows (using Serial Data Line), then this confirms my assumption that all the JP2 does on the Slice of Pi/o is to provide a convenient way to easily connect to the GPIOs. So... I think this confirms that the display will work as I proposed.

I will give this a go tomorrow (if it has arrived in the morning) and see how it goes (and report back).

The website also confirms my thoughts on how to change the addressing of the I2C (if I need to later).

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 7:55 pm

Adafruit uses a MCP23008 ( half of a MCP23017) to drive their 16x2 LCD displays.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 8:14 pm

but I don't want to use the MCP23017 to drive the LCD, I just want to use the remaining GPIOs.

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Wed Jan 01, 2014 10:22 pm

robbo100 wrote:but I don't want to use the MCP23017 to drive the LCD, I just want to use the remaining GPIOs.
Your Slice of Pi does a few good things.

1. It puts a MCP23017 between you and your RPi GPIO pins
2. It gives you 8 GPIO pins with direct connections
3. It gives you 16 extra GPIO pins on the I2C bus

So you have a free choice.
Using I2C is no more difficult than doing direct GPIO programming especially when you use something like Gordon's WiringPi

http://wiringpi.com/
http://blogs.foomatic.org/blink-away-the-tears/
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.


User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Thu Jan 02, 2014 1:27 am

It can get confusing, but it sounds like you're doing fine, Robbo100. 'address' is used for different (related) things: an I2C address identifies a single device, a register address identifies a particular specialized memory location in the device (datasheets will tell you what they mean), a memory address identifies a byte or word of random access memory in the device.

The 0x80, 0x40, 0x20 etc. you see in the code you quoted are actually 'bitmasks', in this case a single 'on' bit used to test whether a certain bit in another byte is on or off. They make more sense if you write them out in binary: '10000000', '01000000', '00100000', etc. So that code is simply saying "If the highest bit of the input byte is on, set this GPIO on, if the next highest bit is on, set that GPIO on, etc. The enable pin is then toggled to let the LCD know you're done writing, kinda like hitting the buzzer in final Jeopardy to lock in your answer. Using just four data lines, it's done twice to write a full byte.

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Thu Jan 02, 2014 9:00 am

Awesome reply Douglas6, thanks.

I suppose the thing that has confused me (now that I understand things better), is that the GPIOs are simply digital outputs, and they seem to be doing something magical when connected to the LCD in order to write words on the screen (I thought they might be connected as a serial bus or something). 4 data pins (either 1 or 0) plus a Register Select and a Enable pin (either 1 or 0), doesn't seem like a lot to write all those letters on the screen, and I didn't realise (even though it was obvious), that the LCD itself has an intelligent chip installed on it, interpreting the sent data.

So, I was worrying that the GPIO lines were doing something more clever like just sending 1s or 0s, because 4 bits is only 15 (and the device can write many more than 15 different shaped characters :lol: ).

However, thinking about it, since it is sending the data in 4 bit mode, it is actually sending 8 bits, which gives it 255 different character options (more than enough for 26 lower case letters, 26 upper case letters, 10 digits, and a whole load of other characters).

So, the code itself, is simply doing the following each time a string is written to it (I think).
- read the letter for the correct position from the string you want to write
- send the high bits to the LCD chip
- toggle enable to write this to the LCD
- send the low bits to the LCD chip
- toggle enable to write this to the LCD
- move to the next register on the LCD
- loop until the whole display is filled

Also, I was getting very confused with the function of the GPIOs. The LCD guide was showing that GPIO 7 and 8 were being used, which weren't shown on my Slice of Pi/o JP2 (or on the Pi header pinout I was looking at). What I didn't realise is that GPIO 7 and 8 are dual use pins, and that they are not being used for their secondary function, but simply to output a 1 or a 0 (as discussed above).

My change in understanding was helped by this sort of picture which shows both the GPIO names, as well as the secondary function names.

Image

With all of this brought together, I now get it all (I think).

At the end of the day, I am trying to code something by borrowing bits of code from others, without understanding what the code is doing and why, which is not particularly good practice! However, this process, my question on this thread, and the answers from everyone, has guided me into researching the areas necessary, and I now see how it all works.

At the end of the day, that is the whole purpose of the Raspberry Pi I suppose - I have learnt something new about computers (albeit, I am not the young target audience of the PRi foundation who they are trying to get into coding).

Since I started all of this (in part) in order to learn how I can teach my kids to write some code (because their school doesn't teach it), I hope it will all help me do a better job of this. And the controller I have built will help me make far better All Grain homebrew beer! :D

Thanks all.

robbo100
Posts: 60
Joined: Thu May 17, 2012 11:39 am

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Thu Jan 02, 2014 9:54 am

Of course, assuming my musings above are correct, you all aready knew all of that stuff. But hopefully it will be useful to someone else who is thinking of getting a 16x2 display to work.

The problem is (for someone like me with a little bit of knowledge), is that a lot of online guides are either very light on information, and just say "wire it up like this and use this code)" (which doesn't help learning much), or are very detailed and use too much jargon for newbies to understand. It is difficult to find a guide which gives enough to help understand what is happening and why, without overwhelming.

Maybe I will write one...

User avatar
Douglas6
Posts: 4783
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Using Slice of Pi/o and 16x2 LCD at the same time?

Thu Jan 02, 2014 2:34 pm

I'd say you've got it, or close enough for now. :) A minor point; 4 bits gives you 16 options, not 15. Remember to start counting at 0. Similarly, 8 bits gives you 256 (0 - 255).

And if you were wondering about that RS (register select) pin. As well as sending character data to the LCD, you can also send commands; clear the display, move the cursor, etc. The RS just lets the LCD know to interpret the data as a character or a command.

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