PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 4:46 pm

Ok, here we go. I ran the install command in Terminal, the install line returned this:-

pi@raspberrypi:~/Adafruit_Python_CharLCD $ sudo python setup.py install
running install
running bdist_egg
running egg_info
creating Adafruit_CharLCD.egg-info
writing requirements to Adafruit_CharLCD.egg-info/requires.txt
writing Adafruit_CharLCD.egg-info/PKG-INFO
writing top-level names to Adafruit_CharLCD.egg-info/top_level.txt
writing dependency_links to Adafruit_CharLCD.egg-info/dependency_links.txt
writing manifest file 'Adafruit_CharLCD.egg-info/SOURCES.txt'
reading manifest file 'Adafruit_CharLCD.egg-info/SOURCES.txt'
writing manifest file 'Adafruit_CharLCD.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-armv7l/egg
running install_lib
running build_py
creating build
creating build/lib.linux-armv7l-2.7
creating build/lib.linux-armv7l-2.7/Adafruit_CharLCD
copying Adafruit_CharLCD/Adafruit_CharLCD.py -> build/lib.linux-armv7l-2.7/Adafruit_CharLCD
copying Adafruit_CharLCD/__init__.py -> build/lib.linux-armv7l-2.7/Adafruit_CharLCD
creating build/bdist.linux-armv7l
creating build/bdist.linux-armv7l/egg
creating build/bdist.linux-armv7l/egg/Adafruit_CharLCD
copying build/lib.linux-armv7l-2.7/Adafruit_CharLCD/Adafruit_CharLCD.py -> build/bdist.linux-armv7l/egg/Adafruit_CharLCD
copying build/lib.linux-armv7l-2.7/Adafruit_CharLCD/__init__.py -> build/bdist.linux-armv7l/egg/Adafruit_CharLCD
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_CharLCD/Adafruit_CharLCD.py to Adafruit_CharLCD.pyc
byte-compiling build/bdist.linux-armv7l/egg/Adafruit_CharLCD/__init__.py to __init__.pyc
creating build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_CharLCD.egg-info/PKG-INFO -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_CharLCD.egg-info/SOURCES.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_CharLCD.egg-info/dependency_links.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_CharLCD.egg-info/requires.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying Adafruit_CharLCD.egg-info/top_level.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/Adafruit_CharLCD-1.1.1-py2.7.egg' and adding 'build/bdist.linux-armv7l/egg' to it
removing 'build/bdist.linux-armv7l/egg' (and everything under it)
Processing Adafruit_CharLCD-1.1.1-py2.7.egg
Removing /usr/local/lib/python2.7/dist-packages/Adafruit_CharLCD-1.1.1-py2.7.egg
Copying Adafruit_CharLCD-1.1.1-py2.7.egg to /usr/local/lib/python2.7/dist-packages
Adafruit-CharLCD 1.1.1 is already the active version in easy-install.pth

Installed /usr/local/lib/python2.7/dist-packages/Adafruit_CharLCD-1.1.1-py2.7.egg
Processing dependencies for Adafruit-CharLCD==1.1.1
Searching for Adafruit-GPIO==1.0.4
Best match: Adafruit-GPIO 1.0.4
Processing Adafruit_GPIO-1.0.4-py2.7.egg
Adafruit-GPIO 1.0.4 is already the active version in easy-install.pth

Using /usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.4-py2.7.egg
Searching for spidev==3.3
Best match: spidev 3.3
Adding spidev 3.3 to easy-install.pth file

Using /usr/lib/python2.7/dist-packages
Searching for Adafruit-PureIO==0.2.3
Best match: Adafruit-PureIO 0.2.3
Processing Adafruit_PureIO-0.2.3-py2.7.egg
Adafruit-PureIO 0.2.3 is already the active version in easy-install.pth

Using /usr/local/lib/python2.7/dist-packages/Adafruit_PureIO-0.2.3-py2.7.egg
Finished processing dependencies for Adafruit-CharLCD==1.1.1
---------------------------------------------------------------------------------------------------------------
I'll move on to the next step now.

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 5:20 pm

2. After installation of LCD library now the Adafruit_python_CharLCD can be used from any python script by using the following line

import Adafruit_CharLCD as LCD​
Note you have to do this at the command line not in python , so assuming you are using a desktop version of raspbian you need to open a terminal window and run the commands in there.
-------------------------------------------------------------------------------------

Yes, I am using the desktop version of Raspian.

I'm not certain whether I properly understood step 2. as above. I ran the import line in Terminal, and got this:-

pi@raspberrypi:~/Adafruit_Python_CharLCD $ import Adafruit_CharLCD as LCD​
bash: import: command not found

I also tried the command when Terminal was in the Home/Pi directory, and had the same result.

Please correct me if I am wrong.

User avatar
DougieLawson
Posts: 39551
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 6:33 pm

You need to add

Code: Select all

#!/usr/bin/python3
as the first line of your program so that bash feeds it into python3.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

pcmanbob
Posts: 9729
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 6:42 pm

Ok so you did the install bit correctly by the look of it , that's the bit you needed to do at the command line in the terminal window.

The import bit is only ever run in a python program, never at the command line.

So try this to test just your LCD, its based on the code in the tutorial so it should work with your LCD as connected.

Code: Select all

import Adafruit_CharLCD as LCD    
import time

lcd_rs        = 25 
lcd_en        = 24
lcd_d4        = 23
lcd_d5        = 17
lcd_d6        = 18
lcd_d7        = 22
lcd_backlight = 4

# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows    = 2

lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
                           lcd_columns, lcd_rows, lcd_backlight)

lcd.message("Hello World")
time.sleep(5)
lcd.message(Raspberry Pi)
time.sleep(10)
lcd.clear()
Please ignore Dougie's suggestion the adafruit code was installed only for python 2 in the tutorial.


Dougie.
Having checked the adafruit git hub page for Adafruit Python CharLCD, it only mentions installing for python 2 , python 3 is never mentioned so I don't know if its supported, but its certainly not installed for python 3, please don't confuse the situation by suggesting the OP use python 3 at this stage.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:24 pm

pcmanbob,

I tried to run your code but had this message:-

>>> %Run 'Adafruit LCD test.py'
Traceback (most recent call last):
File "/home/pi/Desktop/Adafruit LCD test.py", line 21
lcd.message(Raspberry Pi)
^
SyntaxError: invalid syntax
>>>

So, no luck with that I'm afraid: Adafruit LCD test.py does exist in the Desktop directory though.

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:32 pm

Dougie,

I insert: #!/usr/bin/python3 as the first line of code. Running it rreturned this:-

>>> %Run 'MCP4725 code.py'
Traceback (most recent call last):
File "/home/pi/MCP4725 code.py", line 18, in <module>
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
AttributeError: module 'Adafruit_CharLCD' has no attribute 'Adafruit_CharLCD'
>>>

After reading pcmanbob's post, I changed the first line to: #!/usr/bin/python2


The result:-

>>> %Run 'MCP4725 code.py'
Traceback (most recent call last):
File "/home/pi/MCP4725 code.py", line 18, in <module>
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
AttributeError: module 'Adafruit_CharLCD' has no attribute 'Adafruit_CharLCD'
>>>

The same problem.

My brain now hurts!

User avatar
joan
Posts: 14989
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:33 pm

There should be quotes around Raspberry Pi.

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:41 pm

Dougie,

your line: #!/usr/bin/python3 is commented out with the #

I tried deleting the # but got this returned:-

>>> %Run 'Adafruit LCD test.py'
Traceback (most recent call last):
File "/home/pi/Desktop/Adafruit LCD test.py", line 1
!/usr/bin/python3
^
SyntaxError: invalid syntax
>>>
I get the same when changing to python2.

User avatar
rpdom
Posts: 17416
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:45 pm

PhilR100742 wrote:
Sun Nov 10, 2019 8:41 pm
Dougie,

your line: #!/usr/bin/python3 is commented out with the #
No, it isn't. The #! syntax on the first line of a script has a special meaning to the Linux command line (not the Python interpreter shell where you appear to be running it from.)

But as previously mentioned, that code doesn't appear to be support under Python3 yet, just Pythin2 which is the default in Buster.
Unreadable squiggle

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:47 pm

Joan,

Line 22 is this:-

lcd.message(Raspberry Pi)

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:49 pm

Dougie,

#!/usr/bin/python3

Ok, I didn't know that. However, the line is wholly greyed out in the IDE.

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:52 pm

Guys, I'm going to have to leave this for now. I'll get back to it tomorrow.

Thank you everyone for your input. :?

User avatar
DougieLawson
Posts: 39551
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 8:58 pm

You may need to use

Code: Select all

#!/usr/bin/python
since it appears Adafruit aren't living in the same decade as the rest of us. It's a special comment known as a shebang line. https://en.wikipedia.org/wiki/Shebang_(Unix)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

pcmanbob
Posts: 9729
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: MCP4725 DAC with LCD issues

Sun Nov 10, 2019 10:56 pm

PhilR100742 wrote:
Sun Nov 10, 2019 8:32 pm


>>> %Run 'MCP4725 code.py'
Traceback (most recent call last):
File "/home/pi/MCP4725 code.py", line 18, in <module>
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
AttributeError: module 'Adafruit_CharLCD' has no attribute 'Adafruit_CharLCD'
>>>

The same problem.

My brain now hurts!
Not the same problem at least its finding the module now , but there appears to be a problem with the module.

Well the line at which the error is occurring is correct I looked in the example program on the git hub page.

So I have no idea why it should not work , but the program was last updated in sept 2017 and the git hub page has been locked to read only which suggests its been abandoned.

You could ask the writer of the tutorial for help , but I doubt help will be forth coming.

I would suggest you look at an LCD drive module that is still maintained like RPLCD : https://pypi.org/project/RPLCD/
and use that instead to drive you LCD.

Out of interest which version of raspbian are you using ?

Edit...

I tried running the install for the LCD on a fully updated raspbian stretch , it fails with an error which suggests the OS has moved on from the point at which the adafruit LCD module will work.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Mon Nov 11, 2019 4:45 pm

pcmanbob,

Ok, you appear to correct with this. I'll take a look at your suggested alternative. Yes, the video in the tutorial is hopeless; nothing useful in it!

Phil

pcmanbob
Posts: 9729
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: MCP4725 DAC with LCD issues

Mon Nov 11, 2019 4:49 pm

PhilR100742 wrote:
Mon Nov 11, 2019 4:45 pm
pcmanbob,

Ok, you appear to correct with this. I'll take a look at your suggested alternative. Yes, the video in the tutorial is hopeless; nothing useful in it!

Phil
If you need help setting up RPLCD to work with your existing connections , just ask and I will do a write up on installing and setting up .

I would suggest you start over with a fresh install of raspbian stretch or preferably buster, RPLCD will work with python 2 & 3.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PhilR100742
Posts: 24
Joined: Thu Oct 31, 2019 7:52 pm

Re: MCP4725 DAC with LCD issues

Fri Nov 15, 2019 6:03 pm

pcmanbob, I haven't yet done anything further. Your suggestion may well be worth trying, including a fresh Raspbian install. I'll find out about Stretch and Buster, I don't know the differences to the standard full desktop Raspian.

However, the final objective has to be the implementation of the MCP4725 DAC with 16 x 2 mono LCD.

I appreciate your offer of help with the code, that's very kind of you.

I'm rather bogged down with DIY work for the next few days, so I won't have much time to dedicate to Rpi work.

Thanks for everything.

Phil

chipace
Posts: 120
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: MCP4725 DAC with LCD issues

Fri Nov 15, 2019 9:05 pm

If you had bought a LCD with an i2c interface you would not need to re-install anything. In my opinion, the lesson to be learned here is not to buy hardware with a complex gpio interface, when an i2c version is available for a little more $$.
I learned this lesson years ago... and have benefited since.
It is surprising that many large companies still struggle with this lesson.

pcmanbob
Posts: 9729
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: MCP4725 DAC with LCD issues

Fri Nov 15, 2019 10:22 pm

The problem is not the interface but the driver software, the software supports both I2C and direct gpio control.

The problem is that the adafruit driver no longer works so even if an I2C interface was being used it still wouldn't have worked.

The driver I suggested RPLCD again supports both I2C and direct gpio control and also python 2 & 3.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

chipace
Posts: 120
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: MCP4725 DAC with LCD issues

Sat Nov 16, 2019 5:53 am

Here's my i2c driver:

Here is lcddriver.py

Code: Select all

import i2c_lib
import time

class lcd:
   # LCD Address
   ADDRESS = 0x27

   # commands
   LCD_CLEARDISPLAY = 0x01
   LCD_RETURNHOME = 0x02
   LCD_ENTRYMODESET = 0x04
   LCD_DISPLAYCONTROL = 0x08
   LCD_CURSORSHIFT = 0x10
   LCD_FUNCTIONSET = 0x20
   LCD_SETCGRAMADDR = 0x40
   LCD_SETDDRAMADDR = 0x80
	
   # flags for display entry mode
   LCD_ENTRYRIGHT = 0x00
   LCD_ENTRYLEFT = 0x02
   LCD_ENTRYSHIFTINCREMENT = 0x01
   LCD_ENTRYSHIFTDECREMENT = 0x00
	
   # flags for display on/off control
   LCD_DISPLAYON = 0x04
   LCD_DISPLAYOFF = 0x00
   LCD_CURSORON = 0x02
   LCD_CURSOROFF = 0x00
   LCD_BLINKON = 0x01
   LCD_BLINKOFF = 0x00

   # flags for display/cursor shift
   LCD_DISPLAYMOVE = 0x08
   LCD_CURSORMOVE = 0x00
   LCD_MOVERIGHT = 0x04
   LCD_MOVELEFT = 0x00
	
   # flags for function set
   LCD_8BITMODE = 0x10
   LCD_4BITMODE = 0x00
   LCD_2LINE = 0x08
   LCD_1LINE = 0x00
   LCD_5x10DOTS = 0x04
   LCD_5x8DOTS = 0x00
	
   # flags for backlight control
   LCD_BACKLIGHT = 0x08
   LCD_NOBACKLIGHT = 0x00

   En = 0b00000100 # Enable bit
   Rw = 0b00000010 # Read/Write bit
   Rs = 0b00000001 # Register select bit
	
   #initializes objects and lcd
   def __init__(self):
      self.lcd_device = i2c_lib.i2c_device(self.ADDRESS)

      self.lcd_write(0x03)
      self.lcd_write(0x03)
      self.lcd_write(0x03)
      self.lcd_write(0x02)

      self.lcd_write(self.LCD_FUNCTIONSET | self.LCD_2LINE | self.LCD_5x8DOTS | self.LCD_4BITMODE)
      self.lcd_write(self.LCD_DISPLAYCONTROL | self.LCD_DISPLAYON)
      self.lcd_write(self.LCD_CLEARDISPLAY)
      self.lcd_write(self.LCD_ENTRYMODESET | self.LCD_ENTRYLEFT)
      time.sleep(0.2)

   # clocks EN to latch command
   def lcd_strobe(self, data):
      self.lcd_device.write_cmd(data | self.En | self.LCD_BACKLIGHT)
      time.sleep(.0005)
      self.lcd_device.write_cmd(((data & ~self.En) | self.LCD_BACKLIGHT))
      time.sleep(.0001)

   def lcd_write_four_bits(self, data):
      self.lcd_device.write_cmd(data | self.LCD_BACKLIGHT)
      self.lcd_strobe(data)

   # write a command to lcd
   def lcd_write(self, cmd, mode=0):
      self.lcd_write_four_bits(mode | (cmd & 0xF0))
      self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))

   # put string function
   def lcd_display_string(self, string, line,delay=0):
      if line == 1:
         self.lcd_write(0x80)
      if line == 2:
         self.lcd_write(0xC0)
      if line == 3:
         self.lcd_write(0x94)
      if line == 4:
         self.lcd_write(0xD4)

      for char in string:
         self.lcd_write(ord(char), self.Rs)
         time.sleep(delay)

   # clear lcd and set to home
   def lcd_clear(self):
      self.lcd_write(self.LCD_CLEARDISPLAY)
      self.lcd_write(self.LCD_RETURNHOME)
Here is i2c_lib.py

Code: Select all

import smbus
import time

class i2c_device:
   def __init__(self, addr, port=1):
      self.addr = addr
      self.bus = smbus.SMBus(port)

# Write a single command
   def write_cmd(self, cmd):
      self.bus.write_byte(self.addr, cmd)
      time.sleep(0.0001)

# Write a command and argument
   def write_cmd_arg(self, cmd, data):
      self.bus.write_byte_data(self.addr, cmd, data)
      time.sleep(0.0001)

# Write a block of data
   def write_block_data(self, cmd, data):
      self.bus.write_block_data(self.addr, cmd, data)
      time.sleep(0.0001)

# Read a single byte
   def read(self):
      return self.bus.read_byte(self.addr)

# Read
   def read_data(self, cmd):
      return self.bus.read_byte_data(self.addr, cmd)

# Read a block of data
   def read_block_data(self, cmd):
      return self.bus.read_block_data(self.addr, cmd)
Here is lcd_hello_world.py

Code: Select all

import lcddriver
import time
# lcd start
lcd = lcddriver.lcd()
# this command clears the display
lcd.lcd_clear()

# now we can display some characters (text, line)
def simple_display():
	lcd.lcd_display_string("   Hello world !", 1)
	lcd.lcd_display_string("      I am", 2)
	lcd.lcd_display_string("        a", 3)
	lcd.lcd_display_string("   Raspberry Pi !", 4)

Return to “General discussion”