Learn about logic-level switching using a MAX232, how it’s applied in electronics, and how to connect to devices with RS-232 capabilities.
We’ve commonly heard the term logic-level shifting during our endeavours of becoming an electronics guru. Simply put, logic-level shifting is the transition of two states, high and low at the devices specified voltage levels, translating to binary values (bits) and thus, internally used to perform functions within a device.
To communicate these bits between devices, we can either employ a serial or parallel communication method. Serial communication generally transfers 8 bits (1 byte) of information at one single time over a single wire, whereas parallel communication transfers multiple bytes using multiple wires, beneficial for faster data transfers at one single time, however, sacrifices valuable device pins, leaving the user with little selection for local pin allocation.
Serial communication uses minimal pins and ports of a device, and allows for cheaper wire links between devices due to fewer wires needed.
Typically, microcontrollers operate on TTL or CMOS logic-levels using USART (Universal Synchronous/Asynchronous Receiver/Transmitter) for serial communication. Taking an Arduino Uno, for example, communication between two Uno’s is possible using two wires, a transmit and receive line. However, much like talking to a person in a room, the more distant they become, the harder it is to hear and speak to them. This also occurs in electronics, with signal attenuation over the specified distance.
We can use tools to communicate with the person in the room to make our voices louder, such as a megaphone, but what if the person speaks another language we can’t understand. Let’s imagine this situation as electronic devices, where distant wire links can induce high impedance, attenuating the signal at the receiving end of the transmission.
Establishing communication between microcontrollers and personal computers (PC) also inherits issues, as typically the serial communication in microcontrollers operate on 0V to +5V logic and serial communication on PC’s operate at -10V to +0V. This is why we use logic-level converters, to allow devices to communicate on a more efficient communication link and understand each other’s language.
LONG DISTANCE COMMuNICATION USING RS-232
RS-232 is a two-way, direct link, serial communication method, connecting a main controller labelled Data Transmission Equipment (DTE) and its peripheral connections labelled Data Communication Equipment (DCE).
RS-232 has been around for a number of decades now. Its survival in the field can be pointed towards reliability and simplicity. Yes, there are much faster methods of serial communication that far outweigh RS-232, however, RS-232’s lack of bus topology’s and communication speeds is somewhat the reason it has survived over the years.
The ability for a cheap, direct link from a controller to a device means an improvement in system stability, especially in the commercial field. Take an example of a multi-storey building with a single main controller on the bottom floor and multiple devices on every level. Both the devices and main controller support ethernet communication and RS-232. We have the option to either connect all the devices using ethernet and address them appropriately using the local area network, or we can run individual cables from the main controller to each device using RS-232. As the local area network is shared with multiple different devices, including computers with internet access, we can find ourselves running into communication issues, with data collisions in the network a possibility.
Using RS-232 to each device, these issues are avoided, as the only thing on the communication link is the device directly to the main controller. The downside, however, is the cost of running individual cables for each device back to the main controller. But, if system reliability is of high priority, this cost pays itself off with servicing and downtime costs of using ethernet communication. Theoretically, RS-232 cable lengths are limited to a maximum of 10 metres using high data transmission rates, however, the length of cable is determined by the capacitance of the circuit and transmission speed. Using low-capacitance cables and low data transmission rates, lengths of up to 100 metres can be achieved.
This is where we introduce the MAX232 IC, an industry common TTL/CMOS to RS-232 level converter IC. This IC is capable of +30V to -30V from RS-232 and shifts the levels down to 0V to 5V TTL/CMOS, acting as an intermediator between devices with level shifting. The IC can operate off a single 5V power supply, where level shifting to +30V to -30V is done using capacitors as charge pumps. A charge pump is a DC to DC converter where capacitors are used to store electrical energy to raise or lower voltage levels.
The MAX232 is available in four different form factors, three of them surface mounted. Focusing on the Plastic Dual-In-Line (PDIP) package as this form factor allows us to easily mount into a breadboard, with 2.54mm pin spacing.
The MAX232 requires very minimal components for operation. The IC itself features two drivers and two receivers including a capacitive voltage generator to shift up to RS-232 levels from a single 5V supply. Four capacitors are used for the voltage generator, using pins 1 - 6 of the MAX232.
RS-232 Communication using an Arduino Uno
We'll now show you how to connect a MAX232 IC to an Arduino Uno and your device that has an RS-232 port.
If you refer to the following schematic and Fritzing diagrams, you can see the grey, white and black wires ready for connection to an RS-232 device. In our example. the black wire is GND, the grey wire from pin 13 is RX (Receive), and the white wire from pin 14 is Tx (Transmit).
|Parts Required:||Jaycar||Altronics||Core Electronics|
|1 x MAX232 IC||ZK8824||Z9190||002-595-MAX232IN|
|1 x RS-232 to TTL Converter||XC3724||Z6369||CEO4442|
|5 x 1µF Capacitors *||RG5170||R2748B||COM-08375|
|1 x Breadboard||PB8820||P1002||CE05102|
|7 x Male-to-Male Jumper Wires*||WC6024||P1022||ADA1955|
|1 x Arduino Uno or Compatible||XC4410||Z6280||A000066|
|4 x Male-to-Female Jumper Wires*||WC6028||P1017||ADA1952|
|2 x Interconnecting Wires*||PB8850||P1014A||PRT-14671|
|4 x M3 Bolts*||HP0400||H3110A||FIT0061|
|2 x M3 Spacers*||HP0904||H1233||POLOLU-1986|
|1 x D9 Male-to-Female Serial Cable||WC7534||P1770A||CAB-00065|
* Quantity shown, may be sold in packs. You’ll also need a breadboard and prototyping hardware. A D9 gender changer may also be required (see text).
RS-232 can use many different connectors, such as a 5-pin phoenix connector or an RJ12. Most commonly, however, the D9 connector is used. Pinouts for these connections of RS-232 can vary between equipment, however, generally RS-232 follows the pinout shown here.
As we are only concerned with using the MAX232 for TTL/CMOS conversion, the only pins used are 2, 3, and 5 for TX, RX, and GND.
For permanent installations, pre-fabricated PCB’s using the MAX232 and a D9 connector can be purchased from electronics retailers. These modules allow for an easier connection to other devices using D9 connectors.
Following the schematic, connect the Arduino Uno to the MAX232 converter PCB using male-to-female jumper wires. We found two mounting holes towards the rear of the Arduino Uno to perfectly match the spacing on the MAX232 converter PCB, so we mounted it using M3 spacers and bolts for a cleaner solution.
Using a male-to-female D9 serial cable connect the D9 connector of the MAX232 converter PCB to the device, in this case, a PC.
Be mindful, devices aren’t standardised to containing a male or female connector, so a gender changer may be needed.
If you find connection issues or there’s no serial communication, you may have to swap pins 2 and 3 of the RS-232 link, swapping TX and RX. For devices to correctly communicate, TX of the RS-232 line needs to link to RX of the receiving device.
Before we program the Arduino Uno to communicate with the device, we need to find the parameters the device communicates on. Baud-rate determines the communication speed of the serial transmission, denoted as bits per second or bps. Most commonly, devices operate on a 9600 bps baud-rate. The image here illustrates serial data transmission.
The serial transmission begins with a start bit, telling the receiver that data flow has begun. This is followed by the data itself of 1 byte or 8 bits. A parity bit follows the data and is used for error checking followed by a final stop bit, concluding the transmission.
Now we understand the fundamentals of RS-232, we can simply initialise serial communications on the Arduino by writing:
in the setup of Arduino IDE. We change speed to the communication speed of the device and change config if the data lines aren’t 8 bits or parity bits are used. This also selects the number of stop bits used. If you leave it blank, this defaults to 8 bits transmission, no parity and 1 stop bit.
Serial.begin(9600); // Open serial, 9600bps
Find the serial communication port on the PC with RS-232 using device manager.
Open the serial port on the PC using software such as Putty and select the same communication parameters as the Arduino.
Open Arduino serial monitor on the computer connected to the Arduino. Typing messages into the Arduino serial monitor, we can receive messages on the serial of the PC.
You have now successfully communicated using a MAX232 IC.
WHERE TO FROM HERE?
Use the MAX232 converter IC to control devices using the Arduino Uno! Many control devices use RS-232 for instructions on how to operate, so the Arduino is able to be used as a main controller.
Just look around at some devices you have in the house. It could be a laboratory power supply with an RS-232 connection, or an oscilloscope. Just a quick browse through the devices operation manual can determine the baud-rate for communication, what stop bits and if there is parity checking.
It will also tell you commands the device listens for, such as changing voltage on the power supply, or putting it into standby.
There are so many devices with RS-232 so take power and start communicating!