Friday, August 24, 2012
Raspberry PI - More about Serial 3.3v, TTL and RS-232 levels
If you tried to implement the last blog post, one thing that those of you new to electronics may find strange is that when there is NO serial output, the level of the TXD pin is "high" - for the rPI GPIO pin this is around 3.3 volts. For a TTL level serial, this would be around 5 volts. (Don't forget that you have to convert the 3.3 level to 5 if you are interfacing the rPI to TTL logic, as discussed elsewhere in this blog).
When the data is sent, the pin drops unasserted (see Ref 1) for the "start" bit , then up and down for the 1's and 0's of the data (which may be ascii characters, see Ref 2), then back to asserted. The "asserted = 1" logic level is 3.3 or 5 volts, and the "unasserted = 0" logic level is 0 volts. This seems a bit strange, but that is how it works. More on that later down the page.
Here is the waveform (from Ref 3):
There is also another standard for serial data, called RS-232. It is bipolar, with the positive level for the 0's and a negative for the 1's. The positive and negative voltage seings are 3 to 15 volts (Ref 4)
There are off the shelf chips like the MAX232 (Ref 5) that facilitate the conversion to 3.3/5 and the bipolar signal. Some of these chips even use external capacitors to facilitate on board "charge pumps" to develop the +/- RS232 voltages from the 3.3 or 5 volt chip supply.
Now here is the interesting part. The reason that the signal is held at the "asserted = 1" state when there is NO data is a holdover from the old days of mechanical Teleprinter/Teletypes (Ref 6):
" The "idle" condition of the circuit is a continuous marking state, with the start of a character signalled by a "start bit", which is always a space."
This way, of the line connection was broken electrically, it would cause:
"a receiving teleprinter to cycle continuously".
This would be a sign to the operator that the line was down. If it was the other way around, there would be no warning the line was down, and the op would wonder if it was just a quiet day or is there was really a problem.
Now the even more interesting part. This is apparently a carry over from old land line telegraph keys (Ref 7, 8):
" Straight keys used by telegraph operators also had a "shorting bar" that closed the electrical circuit when the operator was not actively sending messages. This was to complete the electrical path to the next station so that the sounder would operate, as in the operator receiving a message from the next town."
So now you know the rest of the story. The state of the art SOC chip in your rPI uses a standard that can trace it's way directly back to the invention of telegraphy in 1836 (or earlier).
References
1) http://en.wikipedia.org/wiki/ASCII
2) http://en.wikipedia.org/wiki/Logic_level
3) http://esd.cs.ucr.edu/labs/serial/serial.html
4) http://en.wikipedia.org/wiki/RS-232
5) http://en.wikipedia.org/wiki/MAX232
6) http://en.wikipedia.org/wiki/Teleprinter
7) http://en.wikipedia.org/wiki/Telegraph_key
8) http://www.linuxmisc.com/3-solaris/b1c64220ef83ca8a.htm
Eric Pierce VA3EP - See the Disclaimer in the Introduction
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
Thursday, August 23, 2012
Raspberry PI - Serial Port Output
The next thing that I wanted to do is get a Python program talking to the serial port TXD pin on the GPIO header (Pin 8) . Using the info in ref 1, here is what to do to get serial output:
To keep from spawning a terminal login session on the header serial port, in the file
/etc/inittab
Comment out the line (using nano as discussed in previous posts)
# 2:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
You can also stop boot up messages as per the reference, but I did not do this.
Then you need to re-boot (as the login process was spawned already, and will not be spawned until you boot with that line commented out)
$ sudo shutdown -r now
I then found that you need to install this software package
$ sudo apt-get install python-serial
When rebooted, log back in and nano up the following Python program as sertest.py (heavily based on ref 1, some neat tricks here, like the code to do a clean exit when you hit ^C on the controlling keyboard):
----------------------------------------------------------------
#!/usr/bin/env python#
print ("Starting program")
import serial
import time
# import string
#
count = 1
test=serial.Serial("/dev/ttyAMA0",9600)
test.open()
teststring = "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz"
#
try:
while True:
print count, teststring
test.write(teststring)
print "Waiting 1 second"
time.sleep(1)
count = count + 1
#
except KeyboardInterrupt:
pass # do cleanup here
#
test.close()
----------------------------------------------------------------
Now run it as root (to get access to the metal):
$ sudo python sertest.py
Observing the serial TXD (header pin 8) with the logic probe, you can see a data stream while the program is running. It works!
References:
Eric Pierce VA3EP - See the Disclaimer in the Introduction
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
Raspberry Pi - Why 74HC14N (or other "buffer" Chip)?
** UPDATED Aug 28, 2012**
The reason for putting a buffer chip between the rPI GPIO pin and external electronics is not only convert the 3.3 to 5 volt (or other) logic levels, but protect it from mistakes like applying a high logic level when the chip is configured as an output (some chips are designed to handle this, other are not). Static discharges are also a concern.
When I mentioned using a 74HC14N to interface to the rPI, reader commented "Amazing that because of these 'new' 3.3v parts we're having to resort to these old 74HCxx parts".
Yes, it IS an old part, but there is a LOT of inertia around 7400 class parts, and a LOT of derivative types like the 74HCXX parts. As this article states "Several generations of pin-compatible descendants of the original family have since become de-facto standard electronic components"
You can get the pinouts and more info for the 74HC14N here. Note that it is a "Hex inverter" which means that it has 6 inverters in the package, and as a bonus it has a Schmitt trigger input, which will tend to clean up slower transitioning signals is you are using them as an input buffer.
The author of the referenced article also stated that there are modern parts that will do the trick, but if you have the old parts hanging around in your junkbox, or you can get them from an eBay seller, then they are fine.
The other issue of course if getting standard DIP parts as so many new components are surface mount , and are difficult or impossible to breadboard with a proto board.
No matter how complex the main chips, unless you have a circuit specifically designed for a project, you always need some "glue chips" to interface things together.
Some of you have worked with Microchip PIC chips which seem quite robust (I have have accidentally drawn a static spark after walking across a dry rug from a screwdriver tip directly to a PIC I/O pin and it continued to work ok). I suspect that the PIC chips have a lot less (relative) complexity than the Broadcom SOC, so I also suspect that the PIC chips have more room to put I/O buffers, protection diodes, etc. which is needed for the type of apps they are used in. The SOC is normally buried in a phone or tablet so probably brings out more or less the raw gate output, with less protection I imagine.
So I am working very carefully around the rPI GPIO pins because of that :-)
Reference:
http://www.petervis.com/Raspberry_PI/Raspberry_Pi_GPIO_Header/Raspberry_Pi_GPIO_Header.html
http://en.wikipedia.org/wiki/Electrostatic_discharge#Damage_prevention_in_electronics
http://en.wikipedia.org/wiki/7400_series
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2551
http://en.wikipedia.org/wiki/Dual_in-line_package
http://en.wikipedia.org/wiki/Surface_mount
Eric Pierce VA3EP - See the Disclaimer in the Introduction
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
http://en.wikipedia.org/wiki/Electrostatic_discharge#Damage_prevention_in_electronics
http://en.wikipedia.org/wiki/7400_series
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2551
http://en.wikipedia.org/wiki/Dual_in-line_package
http://en.wikipedia.org/wiki/Surface_mount
Eric Pierce VA3EP - See the Disclaimer in the Introduction
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
Tuesday, August 21, 2012
Raspberry PI – Safely Interfacing to GPIO Pins
I
screwed a rPI to an old breadboard I had, and bolted an old open
frame analog +5, +/- 12 supply to the back and wired it all up.
In the
last post I got a python program toggling all the I/O ports on and
off at 1 second interval, will send out the code. At this point just
measuring the result with a high impedance logic probe.
Next
problem is interfacing the 3.3v rPI levels to the more common 5 v
TTL. According to my research:
For
5=>3.3. you can use a simple 10k/20k resistor voltage divider. I
have lots of resistors, just have to find them.
For
3.3=>5 This seems to be the cheapest alternative:
http://www.datasheetcatalog.org/datasheet/philips/74HC14N.pdf
Blowing
a $0.50 chip seems to be better than blowing a $35 rPI board :-)
For the next step I want to tie the rPI serial RXD/TXD to the TXD/RSD of a 40 pin PICAXE. Then write a very simple PICAXE program to allow me to use I/O pins on the PICAXE as an "expander" for lots of Digital I/O and Analog in, maybe even use a couple of pins to do PWM, which can be integrated to produce an Analog out. Want to use a simple human readable ascii protocol to marshal data and commands back and forth - http://www.amelek.gda.pl/avr/uisp/srecord.htm
That
protocol is really meant for memory mapped data, but you can easily
extend it to do commands, could even leave out the checksum.
You could always use a CSV format - http://en.wikipedia.org/wiki/Comma-separated_values - but the srecords would be easier to parse as they are more field oriented.
You could always use a CSV format - http://en.wikipedia.org/wiki/Comma-separated_values - but the srecords would be easier to parse as they are more field oriented.
Probably
make it totally polling based to start, i.e. the PICAXE monitors the
serial port, the rPI sends data and/or a command, the PICAXE responds
and reads and/or outputs the ports, and then the PICAXE sends back
any data to the rPI. VERY simple.
Joke is that I once tied together 2 multi $100K ATC simulators using a similar technique (one sim for terminal airspace, the other for precision approach radar) - very simple and easy to test and debug. Nice thing about using a human readable ascii format is that you can hang a dumb terminal (emulator) off the lines to see what is going back and forth for development and debugging (if you slow down the back and forth to human readable speeds :-)
References:
http://www.petervis.com/Raspberry_PI/Raspberry_Pi_GPIO_Header/Raspberry_Pi_GPIO_Header.html
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
Monday, August 20, 2012
Raspberry PI – Writing to GPIO Pins
The
following will get all of the General Purpose I/O pins toggling high
and low.
import RPi.GPIO as GPIO
#
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
#
GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
#
count = 1
while (count < 100):
print ("High")
GPIO.output(3, True)
GPIO.output(5, True)
GPIO.output(7, True)
GPIO.output(8, True)
GPIO.output(10, True)
GPIO.output(11, True)
GPIO.output(12, True)
GPIO.output(13, True)
GPIO.output(15, True)
GPIO.output(16, True)
GPIO.output(18, True)
GPIO.output(19, True)
GPIO.output(21, True)
GPIO.output(22, True)
GPIO.output(23, True)
GPIO.output(24, True)
GPIO.output(26, True)
time.sleep(1)
print ("Low")
GPIO.output(3,False)
GPIO.output(5,False)
GPIO.output(7,False)
GPIO.output(8,False)
GPIO.output(10,False)
GPIO.output(11,False)
GPIO.output(12,False)
GPIO.output(13,False)
GPIO.output(15,False)
GPIO.output(16,False)
GPIO.output(18,False)
GPIO.output(19,False)
GPIO.output(21,False)
GPIO.output(22,False)
GPIO.output(23,False)
GPIO.output(24,False)
GPIO.output(26,False)
time.sleep(1)
print ("End")
-----------------------------------------------------------------------------
http://elinux.org/RPi_Low-level_peripherals#General_Purpose_Input.2FOutput_.28GPIO.29
http://elinux.org/RPi_Low-level_peripherals#P2_header
DO NOT
connect leds directly to the pins, as the 3.3 volt rPI I/O pins are
NOT buffered and you can blow the rPI board!
If you
want to drive leds or other loads, you will need buffer electronics.
(I used a high impedance logic probe to observe the pin outputs).
1)
Install Python Development libraries (if not already installed)
$
sudo apt-get install python-dev
( ref:
http://nickhumphreyit.blogspot.ca/2011/10/buildbot-install-error.html
)
2)
Install RPi.GPIO Python Library (if not already installed)
Step 1 –
Download the library
$
wget
http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.3.1a.tar.gz
Step 2 –
Extract the archive to a new folder
$
tar zxf RPi.GPIO-0.3.1a.tar.gz
Step 3 –
Browse to the new directory
$
cd RPi.GPIO-0.3.1a
Step 4 –
Install the library
$
sudo python setup.py install
Step 5 –
Remove the directory and its contents
$
cd ..
$
sudo rm -rf RPi.GPIO-0.3.1a/
Step 6 –
Delete the archive file
$
rm RPi.GPIO-0.3.1a.tar.gz
( ref:
http://www.raspberrypi-spy.co.uk/2012/05/install-rpi-gpio-python-library/
)
3) Nano
up this python program as "leds.py"
(If you
want to make it shorter you can leave out some of the pins)
-----------------------------------------------------------------------------
import
timeimport RPi.GPIO as GPIO
#
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
#
GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
#
count = 1
while (count < 100):
print ("High")
GPIO.output(3, True)
GPIO.output(5, True)
GPIO.output(7, True)
GPIO.output(8, True)
GPIO.output(10, True)
GPIO.output(11, True)
GPIO.output(12, True)
GPIO.output(13, True)
GPIO.output(15, True)
GPIO.output(16, True)
GPIO.output(18, True)
GPIO.output(19, True)
GPIO.output(21, True)
GPIO.output(22, True)
GPIO.output(23, True)
GPIO.output(24, True)
GPIO.output(26, True)
time.sleep(1)
print ("Low")
GPIO.output(3,False)
GPIO.output(5,False)
GPIO.output(7,False)
GPIO.output(8,False)
GPIO.output(10,False)
GPIO.output(11,False)
GPIO.output(12,False)
GPIO.output(13,False)
GPIO.output(15,False)
GPIO.output(16,False)
GPIO.output(18,False)
GPIO.output(19,False)
GPIO.output(21,False)
GPIO.output(22,False)
GPIO.output(23,False)
GPIO.output(24,False)
GPIO.output(26,False)
time.sleep(1)
print ("End")
-----------------------------------------------------------------------------
4) Run
the python program (sudo runs it at root to get access to the metal )
$
sudo python leds.py
Note
that the pin numbering is the actual pin number of the GPIO ribbon
connector, NOT the GPIO number (this had me scratching my head for a
while).
References:
http://elinux.org/RPi_Low-level_peripherals#General_Purpose_Input.2FOutput_.28GPIO.29
http://nickhumphreyit.blogspot.ca/2011/10/buildbot-install-error.html
need to install python dev 1st
http://elinux.org/RPi_Low-level_peripherals#P2_header
Eric Pierce VA3EP - See the Disclaimer in the Introduction
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
Sunday, August 19, 2012
Raspberry PI – Display Time On rPI Web Server (quick hack)
Here is
a python program for the rPI that will generate a new index.html web
page every 5 seconds (yes, very crude way of doing it, but it works
:-)
import datetime
import time
count = 0
while (count<1000):
out_file = open("/var/www/index.html", "wt")
now=datetime.datetime.now()
print ("Writing file, Date and time = ", str(now))
out_file.write("<html><body><h1>It works!</h1>\n")
out_file.write("<p>This is Eric Pierce's Raspberry Pi Webserver.</p>\n")
out_file.write("<p>Date and time = " + str(now) + "</p>\n")
out_file.write("</body></html>\n")
out_file.close()
time.sleep(5)
print ('End')
Eric Pierce VA3EP - See the Disclaimer in the Introduction
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
=============================================
#!/usr/bin/python3
print
('Start')import datetime
import time
count = 0
while (count<1000):
out_file = open("/var/www/index.html", "wt")
now=datetime.datetime.now()
print ("Writing file, Date and time = ", str(now))
out_file.write("<html><body><h1>It works!</h1>\n")
out_file.write("<p>This is Eric Pierce's Raspberry Pi Webserver.</p>\n")
out_file.write("<p>Date and time = " + str(now) + "</p>\n")
out_file.write("</body></html>\n")
out_file.close()
time.sleep(5)
print ('End')
=============================================
If that
is saved in your default folder with the name "webtest.py"
you can run it as so (it needs root privs to do this, that is what
the sudo prefix does):
$ sudo
python3 webtest.py
Then if
you connect to the raspberry pi with a browser from another computer, you see the page with the time.
If you hit the browser refresh button evety 5 seconds, you will see
the time, date, etc.
Only a
few more lines of code to read digital events in your house from
switches, and a bit more to read analog values. Open up a port in
your firewall, and you can access it from the world.
With a
bit more work, you could put buttons on the page to control things!
Cool!!!
Eric Pierce VA3EP - See the Disclaimer in the Introduction
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
Friday, August 17, 2012
Raspberry PI – Web Server
Installing
a web server on the rPI is easy:
$
sudo groupadd www-data
$
sudo apt-get update
$
sudo apt-get install apache2
The web
server comes up and you can attach to the rPI by TCP/IP from any computer on the LAN. If I wanted to open
a port in my firewall, anyone in the world could get to it (BUT this would be a security risk).
You can
not edit the default page with
$
sudo nano /var/www/index.html
Amazing!!!!
Eric Pierce VA3EP - See the Disclaimer in the Introduction
© Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", 1952-2099. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Eric Pierce and "VA3EP Amateur Radio And Other Geek Pursuits", with appropriate and specific direction to the original content.
Subscribe to:
Posts (Atom)
