The RPi has a number of options for connecting to other devices including a serial interface using a universal asynchronous receiver/transmitter (UART) over the General-purpose input/output (GPIO) interface. The UARTs is used with communication standards such as EIA, RS-232, RS-422 or RS-485. The universal indicates that the data format and transmission speeds are configurable. The electric signalling levels and methods (such as differential signalling etc.) are handled by a driver circuit external to the UART. The GPIO on the Raspberry Pi is used to provide an interface to the world, it provides the following functionality through the double row of 26 pins.
- Genuine GPIO (General Purpose Input Output) pins -
- I2C interface pins
- SPI interface, a similar concept to I2C but a different standard
- Serial Rx and Tx pins for communication with serial peripherals
The Serial Peripheral Interface (SPI) bus is a synchronous serial data link, that operates in full duplex mode. It is used for short distance, single master communication, for example in embedded systems, sensors, and SD cards.Devices communicate in master/slave mode where the master device initiates the data frame. Multiple slave devices are allowed with individual slave select lines
I2C (Inter-Integrated Circuit) is a multimaster serial single-ended computer bus. It uses only two bidirectional open-drain lines, Serial Data Line (SDA) and Serial Clock Line (SCL). The I2C reference design has a 7-bit or a 10-bit address space.
I will be looking at uses of SPI and I2C later in another posting.
Signalling levels
Different signalling technology uses different voltages to indicate a logical '1' or '0', along with different device designs. For example RS232 uses a signal between +3v and +25v for a '1' and -3v and -25v for a '0' and RS485 uses a voltage differential.Device | Logic '1' | Logic '0' |
---|---|---|
RPi | +3.3v | 0v |
Arduino(*) | +5v | 0v |
(*) Most Arduino are 5v, a few may be 3.3v
When using sensors for the Arduino with the RPi then you need to care with the signal voltages to prevent damage to the RPi.
Voltage warning
The RPI although having a 5V supply operates on 3.3v and connecting devices that use other voltages can damage the RPI board and components. Care must be taken when interfacing to other devices. The signalling voltages can damage the RPi and in particular if a power feed is used from another device this can damage the RPi as well.When interconnecting devices and sensors to the RPi, care is required to ensure compatible voltage levels are used, and if necessary if voltages levels cannot be matched a logic level converter might be required such as the Sparkfun Logic Level Converter https://www.sparkfun.com/products/11978, I have a number of these myself and I do recommend them, however there are other devices and other methods of connecting different signalling voltages.
Powering the RPi through the GPI
It is possible to supply a 5v power connection on the GPIO pin. BUT, it has no backward protection and it was not really designed to be a 5volt input pin. The 3.3v pin can also be powered with 3.3v as the regulator has build in protection, but again it leaves your BCM2835 unprotected! Typically any power pins on GPIO area are used to power extended circuits.Often this will be done as part of building a headless node (no keyboards, mouse or monitor) allowing the RPi to be powered via a serial interface or other connection.
One man's TX is another man's RX
I have often seen comment that when people have connected TTL to USB leads they had to switch the signal leads around. When connecting devices and leads together you can't just connect the labels. It needs a bit of thought.A Transmission (TX) needs to be received (RX) and for two way communication, the remote TX needs to be connect to the local RX, this requires a crossover in the connection.
This can be combined with a logic level converter, when connecting pay attention to the direction of signal possible through the logic level converter, some channels may be unidirectional, others may be bidirectional.
RPi and 2 slices of serial
The RPi can use the serial connection in two modes- Console access
- Serial Communication
Problems in using serial with other devices
If when trying to use the serial lines to communicate with other devices, the serial port generates errors about device not respond, not available and generally misbehaviours it is often as the serial lines are not under the sole control of the application being run, but still being controlled by the OS as part of console access. In order for reliable use the configuration of the serial lines needs to be configured.Configuring the serial port
By default the RPi is configured to use the serial in a console mode in Raspbian and other distros for the RPi, the configuration files that control the behaviour of the serial connections are the:-/boot/cmdline.txt
/etc/inittab
These files need to me modified as follows. It is recommend that you copy the original files, prior to modifying them.
Modify the cmdline.txt file to remove reference to the ttyAMA0 device.
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
And the inittab file needs to have references to the ttyAMA0 device commented out.
#Spawn a getty on Raspberry Pi serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Serial as a console connection
The initial configuration of the serial port allows it to be used for console access, allowing access to boot messages and providing a text based interface. Connection to the serial pins can be made with a TTL to USB cable such as the Adafruit cable. In addition to the cable if you are connecting to a PC you need your favourite terminal program such as Putty and it needs to connect to the serial port being used by the connection and configured to talk to the RPi. The Comm or serial port used by a TTL to USB cable can be found in Windows by examining the Device Manager screen.RPi Default settings
Speed: 115200
Bits: 8
Parity: None
Stop Bits: 1
Flow Control: None
Programming the serial port
If the serial connection has been freed from the OS as described above, it can be used reliable within your own applications to interface with sensors and other devices.The serial port is registered as a device using the name /dev/ttyAMA0 within the Raspbian distro, and similar names are used for other distros.
The port will often need to be configured and various opensource programme libraries are available for common programming languages that will help with configuring and programming the port.
From the command line the following can be used
stty -F /dev/ttyAMA0 9600
To configure the serial port and data can be sent or received easily using the following.
echo "hello, world" > /dev/ttyAMA0 # send a message
cat /dev/ttyAMA0 # listen for a response
Hopefully the above has been useful, if you feel anything else needs to be added or explained, please drop me a comment.
No comments:
Post a Comment