Page 1 of 2
Re: hcsr-04 how to fixed string
Posted: Sun Jan 26, 2014 9:00 pm
by danjperron
print " Distance : %4.1f cm" % distance
Total of 4 characters with one decimal. The dots and the decimal are parts of the total.
Daniel
Re: hcsr-04 how to fixed string
Posted: Sun Jan 26, 2014 10:59 pm
by danjperron
Maybe
print " Distance :%5.1f cm" % distance
will be more adequate since it will take care of values >99.9 cm also.
Screen display information is somewhat very restricted.
Daniel
Re: hcsr-04 how to fixed string
Posted: Tue Jan 28, 2014 6:31 am
by Onira
can you tell me how did You connect it to pi, how many resistors did You use and the whole script that You used?
I tried to use HC-SR04 but it was killing my RTC DS1307
Re: hcsr-04 how to fixed string
Posted: Tue Jan 28, 2014 11:59 am
by danjperron
Yes I used them a lot . But this was 20 years ago.
it was TTL serial made by Seiko.
I should have one . I will try to find it! No sure if I still have it.
I do have a new one , 4x20. which you could choose the interface.
Daniel
Re: hcsr-04 how to fixed string
Posted: Wed Jan 29, 2014 6:31 am
by Onira
can I ask just for a simple HOW-TO?
Re: hcsr-04 how to fixed string
Posted: Wed Jan 29, 2014 11:51 am
by danjperron
Which interface you like to use? I2C, SPI or TTL serial?
Daniel
Re: hcsr-04 how to fixed string
Posted: Wed Jan 29, 2014 1:02 pm
by Onira
Re: hcsr-04 how to fixed string
Posted: Thu Jan 30, 2014 4:11 am
by danjperron
I didn't see any lcd display from the url you provide.
This is an example of LCD class I just did, (took me 1/2 hour to type) and of course 1 hour to debug and make the Test program.
There are 2 python files,
MyLCD.py which is a class to encapsulate the serial TTL at 9600 baud to communicate with the LCD display.
LCDTest.py which is the test code.
The physical layout is just GND, VCC and the TX, pin 8, to the serial IN of the display. The display is 5V but it works very well with the 3.3V signal.
The display is a Newhaven Display 4x20 which could be program in I2c,SPI and presently in TTL serial.
MyLCD.py
Code: Select all
pi@Pi2 ~ $ cat MyLCD.py
import time
import serial
class MyLCD():
def __init__(self):
self.ser = serial.Serial(port='/dev/ttyAMA0',baudrate=9600)
def write(self,val):
self.ser.write(val)
def ClearScreen(self):
self.ser.write('\xFE\x51')
def DisplayOn(self):
self.ser.write('\xFE\x41')
def DisplayOff(self):
self.ser.write('\xFE\x42')
def SetCursor(self,val):
self.ser.write('\xFE\x45')
self.ser.write(chr(val))
def CursorHome(self):
self.ser.write('\xFE\x46')
def UnderlineCursorOn(self):
self.ser.write('\xFE\x47')
def UnderlineCursorOff(self):
self.ser.write('\xFE\x48')
def MoveCursorLeft(self):
self.ser.write('\xFE\x49')
def MoveCursorRight(self):
self.ser.write('\xFE\x4A')
def BlinkingCursorOn(self):
self.ser.write('\xFE\x4B')
def BlinkingCursorOff(self):
self.ser.write('\xFE\x4C')
def Backspace(self):
self.ser.write('\xFE\x4E')
def SetContrast(self,val):
self.ser.write('\xFe\x52')
self.ser.write(chr(val))
def SetBacklight(self,val):
self.ser.write('\xFE\x53')
self.ser.write(chr(val))
def LoadCustomCharacter(self, customChar, val):
self.ser.write('\xFE\x54')
self.ser.write(chr(customChar))
Tlen = len(val)
if Tlen>8:
Tlen=8
for i in range(Tlen):
self.ser.write(chr(val[i]))
for i in range(Tlen,8,1):
self.ser.write(chr(0))
def MoveDisplayLeft(self):
self.ser.write('\xFE\x55')
def MoveDisplayRight(self):
self.ser.write('\xFE\x56')
def ChangeBaudRate(self,val):
self.ser.write('\xFE\x61')
self.ser.write(chr(val))
def ChangeI2CAddress(self,val):
self.ser.write('\xFE\x42')
self.ser.write(chr(val))
def DisplayVersion(self):
self.ser.write('\xFE\x70')
def DisplayBaudRate(self):
self.ser.write('\xFE\x71')
def DisplayI2CAddress(self):
self.ser.write('\xFE\x72')
and LCDTest.py
Code: Select all
pi@Pi2 ~ $ cat TestLCD.py
import time
import MyLCD
import signal
import sys
ser = MyLCD.MyLCD()
def signal_handler(signal, frame):
ser.ClearScreen()
ser.ClearScreen()
ser.write('Bye!')
sys.exit(0)
def DisplayCounter(counter):
#specify First Line position 8
ser.SetCursor(8)
ser.write('%-6dcycles' % (counter))
def DisplayTime():
#sepcify third line position 5
ser.SetCursor(0x19)
ser.write(time.strftime("%H:%M:%S"))
#insert control-C handler
signal.signal(signal.SIGINT, signal_handler)
#display start message
ser.ClearScreen()
ser.DisplayOn()
ser.UnderlineCursorOff()
ser.write('LCD DISPLAY Test.')
#move to second line
ser.SetCursor(0x40)
ser.write('From Raspberry Pi')
#create a custom character
#just a horizontal line in middle of character
ser.LoadCustomCharacter(1,[0,0,0,0xff,0xff,0,0,0])
#move to third line
ser.SetCursor(0x14)
for i in range(20):
ser.write(chr(1))
#move to fourth line
ser.SetCursor(0x54)
datestring = time.strftime("%d/%m/%y %H:%M:%S")
ser.write(datestring)
time.sleep(3.0);
ser.ClearScreen()
ser.write('Counter:')
ser.SetCursor(0x14)
ser.write('Time:')
counter = 0
while(True):
DisplayCounter(counter)
DisplayTime()
counter=counter+1
time.sleep(0.5)
P.S. Be sure to disable the debug console on /dev/ttyAMA0 in /boot/cmdline.txt and disable the /dev/ttyAMA0 login in /etc/inittab (The last line).
Also change the group owner of /dev/ttyAMA0 to 'dialout' since 'pi' is part of this group
Have fun
Re: hcsr-04 how to fixed string
Posted: Thu Jan 30, 2014 12:25 pm
by Onira
that's right, I didn't use a display yet with hc-sr04
I will try your method and the display I have which is 16x2 HD44780 standard display
thank You

Re: hcsr-04 how to fixed string
Posted: Thu Jan 30, 2014 1:52 pm
by danjperron
Looks like that this lcd display uses 4/ 8 bits bus. So you will need to add a function to write the data using gpio instead of serial.
This is the same type of lcd I was using 25 years ago with Z8, not Z80, and 8051 cpu. I don't I have one anymore.
Daniel
Re: hcsr-04 how to fixed string
Posted: Thu Jan 30, 2014 2:11 pm
by Onira
add a function
? 
Re: hcsr-04 how to fixed string
Posted: Thu Jan 30, 2014 6:01 pm
by danjperron
Yes the code I gave you is for a serial interface.
The Hitachi LCD display use 4 or 8 bits bus.
Instead of using the serial port you will have to send one byte a a time using 8 GPIOs and one more for writing signal.
If you don't have too much GPIO left , you could use the SPI with a serial shift register like the 74hC595 with one of the available SPI chip select.
I Will see if I have one lcd display like this. But I don't think so.
Daniel
Re: hcsr-04 how to fixed string
Posted: Thu Jan 30, 2014 6:33 pm
by danjperron
I found an old Densitron display using the same Hitachi I.C.
So we are in business if the display works. I do have some 74hc595.
Re: hcsr-04 how to fixed string
Posted: Thu Jan 30, 2014 8:39 pm
by Onira
I also have a TFT LCD 2,2" with touchscreen from waveshare connected to my DVK511 board but I can't make it work (the driver loads properly but no display)
still I don't know how to connect HC-SR04 and not crash RTC DS1307
do you use any RTC?
Re: hcsr-04 how to fixed string
Posted: Fri Jan 31, 2014 2:37 am
by danjperron
Ok the Densitron was broken. ;-(
I do have another one, but not with me. So It will have to wait a couple of days.
This is a small schematic on how to connect the LCD with the raspberry PI.
I'm sure that someone already made a design like this one to connect this kind of lcd. It is the simplest thing to do.
Maybe do a search on LCD and hc595.
Daniel
Re: hcsr-04 how to fixed string
Posted: Fri Jan 31, 2014 6:16 am
by Onira
I was thinking about a direct connection to the RPi
now I see I need to buy HC595
Re: hcsr-04 how to fixed string
Posted: Sat Feb 01, 2014 2:22 am
by danjperron
Re: hcsr-04 how to fixed string
Posted: Sat Feb 01, 2014 4:12 am
by danjperron
I bought a LCD2002A at the nearest electronic surplus.
I debug the code and now it is working with the SPI device using the 74HC595.
I just modify the adafruit example to implements the SPI+74HC595.
B.T.W. My previous posted schematic is ok.
LCD_H595.py
Code: Select all
#!/usr/bin/python
#
# based on code from lrvick and LiquidCrystal
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
#
# took from Adafruit github
#https://raw.github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/master/Adafruit_CharLCD/Adafruit_CharLCD.py
#modify By Daniel Perron, January 31, 2014 to implement 74HC595 version using SPI
from time import sleep
import spidev
class LCD_H595():
# 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
# 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
def __init__(self, pin_rs=22, pin_e=18,GPIO = None):
# Emulate the old behavior of using RPi.GPIO if we haven't been given
# an explicit GPIO interface to use
if not GPIO:
import RPi.GPIO as GPIO
self.GPIO = GPIO
self.pin_rs = pin_rs
self.pin_e = pin_e
self.GPIO.setmode(GPIO.BOARD)
self.GPIO.setup(self.pin_e, GPIO.OUT)
self.GPIO.setup(self.pin_rs, GPIO.OUT)
self.spi = spidev.SpiDev()
self.spi.open(0,0)
self.write4bits(0x38) # initialization
self.write4bits(0x0E) # initialization
self.write4bits(0x06) # 2 line 5x7 matrix
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
self.write4bits(0x06) # shift cursor right
self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
self.displayfunction = self.LCD_8BITMODE | self.LCD_2LINE | self.LCD_5x8DOTS
""" Initialize to default text direction (for romance languages) """
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
self.clear()
def begin(self, cols, lines):
if (lines > 1):
self.numlines = lines
self.displayfunction |= self.LCD_2LINE
self.currline = 0
def home(self):
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
self.delayMicroseconds(3000) # this command takes a long time!
def clear(self):
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time
def setCursor(self, col, row):
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
if ( row > self.numlines ):
row = self.numlines - 1 # we count rows starting w/0
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
def noDisplay(self):
""" Turn the display off (quickly) """
self.displaycontrol &= ~self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def display(self):
""" Turn the display on (quickly) """
self.displaycontrol |= self.LCD_DISPLAYON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noCursor(self):
""" Turns the underline cursor on/off """
self.displaycontrol &= ~self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def cursor(self):
""" Cursor On """
self.displaycontrol |= self.LCD_CURSORON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noBlink(self):
""" Turn on and off the blinking cursor """
self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def noBlink(self):
""" Turn on and off the blinking cursor """
self.displaycontrol &= ~self.LCD_BLINKON
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
def DisplayLeft(self):
""" These commands scroll the display without changing the RAM """
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
def scrollDisplayRight(self):
""" These commands scroll the display without changing the RAM """
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
def leftToRight(self):
""" This is for text that flows Left to Right """
self.displaymode |= self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
def rightToLeft(self):
""" This is for text that flows Right to Left """
self.displaymode &= ~self.LCD_ENTRYLEFT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def autoscroll(self):
""" This will 'right justify' text from the cursor """
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def noAutoscroll(self):
""" This will 'left justify' text from the cursor """
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
def WriteByte(self, val, char_mode=False):
self.delayMicroseconds(50) # 50 microsecond sleep
self.GPIO.output(self.pin_rs, char_mode)
self.spi.xfer([val])
self.pulseEnable();
if not char_mode:
self.delayMicroseconds(2000) #command ? then 2000 microsecond sleep
def write4bits(self, bits, char_mode=False):
self.WriteByte(bits,char_mode)
def delayMicroseconds(self, microseconds):
seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds
sleep(seconds)
def pulseEnable(self):
self.GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
self.GPIO.output(self.pin_e, True)
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
self.GPIO.output(self.pin_e, False)
self.delayMicroseconds(1) # commands need > 37us to settle
def message(self, text):
""" Send string to LCD. Newline wraps to second line"""
for char in text:
if char == '\n':
self.write4bits(0xC0) # next line
else:
self.write4bits(ord(char),True)
if __name__ == '__main__':
lcd = LCD_H595()
lcd.clear()
lcd.message(" LCD 16x2\n SPI + 74HC595")
Re: hcsr-04 how to fixed string
Posted: Sat Feb 01, 2014 10:19 am
by Onira
I do have the 16x2 display that was described in adafruit's article
but I also have the DVK511 board from waveshare and the 2,2" TFT LCD display with touchscreen dedicated to that board
I'm just a noob at programming

Re: hcsr-04 how to fixed string
Posted: Sat Feb 01, 2014 1:09 pm
by danjperron
Is this display use SPI ? looks like it. Than it is way easier.
And since you have that interface board , you don't need that hc595.
But the python code will be way different from the Adafruit or my TTL serial .
Check
http://projects.pithan.net/lcd-touch-module/
In the touch screen category, I prefer to use more intelligent display. product like
http://www.4dsystems.com.au/product/5/3 ... _43_PT_PI/
With this system, it is possible that you won't need a raspberry Pi at all. Just the display will do.
Daniel
Re: hcsr-04 how to fixed string
Posted: Sun Feb 02, 2014 1:25 pm
by Onira
the link you gave is for that display that I have with my RPi&DVK511
but it doesn't work for me

Re: hcsr-04 how to fixed string
Posted: Sun Feb 02, 2014 1:36 pm
by danjperron
I don't have your board and your display.
But first go step by step.
1 - Just use the Rpi and the display first. Don't use your add-on board.
2 - Check the display first. and after check the touch screen.
If you plug everything and hope that everything will works. It won't.
Daniel
Re: hcsr-04 how to fixed string
Posted: Sun Feb 02, 2014 2:13 pm
by Onira
the image given with the DVK511 board shows that the display is ok
Re: hcsr-04 how to fixed string
Posted: Sun Feb 02, 2014 2:22 pm
by danjperron
Well an image is an image,
You have to do the works. So start small and add little by little.
Daniel
Re: hcsr-04 how to fixed string
Posted: Sun Feb 02, 2014 2:40 pm
by Onira
okay...
I found some wires and connected the display directly to RPi pins as You suggested
doesn't work
it only lights when I turne the power on...