UART on RPi - Page 1
Forum section: electronics / general

author: PerranOak
author_rank: Frequent Contributor
timestamp: April 22, 2021, 04:25:00 pm
content: I just can't seem to get my @rse in gear as regards UART. I understand the principles but need, actually, to do it!Obviously, I need two "things" that can communicate with each other using UART and I need one, at least, to have a display so that I can see what's happening.My idea is to use the RPi (easily connects to screen, keyboard, etc.) and ... well, something.Does anyone have any ideas how I can get started please including suggestions of to what "thing" I can connect the RPi for practice.Cheers.

author: Twoflower
author_rank: Frequent Contributor
timestamp: April 22, 2021, 04:48:16 pm
content: You can talk to yourself by simply wire Tx-->Rx. If you have a FTDL or SIL USB-UART adapter you can use that to talk to a terminal on the PC. If you do that you'll be surprised that you actually see messages floating by. Especially during boot. The Kernel uses the build in UART for messages.Configuring the build in UART (/dev/ttyAMA0) for bash usage:# Setup ttyAMA0 to 115200 baud, 8n1, no flow controlstty -F /dev/ttyAMA0 115200 cs8 -cstopb -ixon -ixoff -crtscts -hup -parenb -echoIf you want do send messages from bash a simpleecho -ne "Hello World\n" > /dev/ttyAMA0receiving is can be done as well:cat /dev/ttyAMA0Disabling the Kernel using the UART depends a bit on the Distribution. But changing the /boot/cmdline.txt is a starting point (for some reasons removing the tty0 parts didn't last, so I told the kernel to use tty1). This is from Archlinux:root=/dev/mmcblk0p2 rw rootwait audit=0 console=tty1 kgdboc=tty1 rootfstype=ext4 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 elevator=noopEventually you need to stop some services to really tell the kernel you really don't want any kernel messages there like getty@tty0.service (also changed to tty1). But that depends on the used distribution.Maybe I did something wrong and others have better solutions. But it works for me ;-)

author: PerranOak
author_rank: Frequent Contributor
timestamp: April 23, 2021, 11:03:27 am
content: Thank you.It's very kind of you to post but I don't understand half (more?) of your post. Perhaps I'm not ready for serial comms.

author: Twoflower
author_rank: Frequent Contributor
timestamp: April 23, 2021, 11:15:40 am
content: No worries! Its not that complicated as it look. It is just that the RasPi occupies the UART. If you skip that, that reduces to simple read/write instructions. Very much like the keyboard or screen.Maybe using the bash is a bit exotic. If you use C, Python... you can use any example you‘ll find and it will work on the RasPi just fine.If you have a Scope or logic analyzer with an UART decoder build in you can also use this to see what‘s send.

author: Ian.M
author_rank: Super Contributor
timestamp: April 23, 2021, 11:35:33 am
content: Most Pi boards are a poor choice for mucking around with serial comms on their GPIO headers. Seehttps://www.raspberrypi.org/documentation/configuration/uart.mdBefore Pi 4, any Pi with Bluetooth had the full spec. UART allocated to the Bluetooth chip, and using the remaining mini-UART requires you to set a fixed clock frequency, crippling performance, to get a stable baud rate. Also its got limited handshake lines which are a PITA to enable. The PI4's got plenty of full UARTs, so you can avoid the mini-UART, but its relatively high cost makes it a poor choice for experimenting as any mistake has a high probability of killing it. Also 3.3V levels + no 5V tolerance isn't experiment-friendly.A USB<=>serial adapter is a much better choice - unless you are really unlucky/careless you are unlikely to damage the host computer. As a bonus, some logic leve ones (i.e. without RS232 level converter/line drivers) have fully customizable I/O voltage levels. N.B. RS232 line level signals are inverted with respect to logic level UART signals.There is some benefit in using a real COM port on a PC with legacy ports, as RS232 compliant ports can tolerate up to +/-25V signal levels, are short-circuit proof, and dont have a vulnerable 5V Vbus supply so the odds of damaging the PC are minimal. Add a MAX3232 or similar logic level <=> RS232 level converter if you need 5V or 3.3V logic levels.As for what to connect to, the RX and TX pins on just about any Arduino with a seperate USB interface chip are connected to the MCU's UART. Most GPS modules have a UART interface. Most consumer GPSes use a NMEA-0183 serial interface which is quasi-RS232 compatible. Otherwise it depends on what legacy hardware with a UART interface you (a) have available, and (b) are willing to put some effort into hacking.

author: PerranOak
author_rank: Frequent Contributor
timestamp: April 23, 2021, 03:49:41 pm
content: Thanks both.Right, I guess it's best to explain myself fully and what I really want to do, ultimately.I have a project that is a PIC measuring/storing/displaying temperature. Readings are taken over a long period of time and stored in the (16F1827) EEPROM. I then review these one at a time on the "built-in" display - I have to write these down!What I want to do is get these stored data to upload to the PC and eventually into Excel.I thought that RPi and UART were the easiest way. Now, I'm not so sure.

author: Twoflower
author_rank: Frequent Contributor
timestamp: April 23, 2021, 04:08:26 pm
content: That sounds like a reasonable usage case for an UART. Other interfaces might not that easy do debug (e.g. SPI). As starting point I would get a USB-SPI dongle and start with the PIC. A scope might be helpful that you‘re not completely blind if nothing gets visible in the terminal.If that‘s working you can move to the RasPi part. As my RasPi UART experience is based on the good old 1b+ (one of the lowest power RasPi with Ethernet) my advice might be outdated a bit

author: Ian.M
author_rank: Super Contributor
timestamp: April 23, 2021, 05:17:08 pm
content: Is the PIC16F1827 temperature logger widget either portable or located near the PC?If so, adding a serial interface and using a USB <=> logic level serial cable may be a good option. However you still have to capture the data on the PC end, which will involve either writing a custom utility, or some very gnarly VBA scripting in Excel (Microchip don't make it easy to access legacy ports), or using theAACkeysaccessibility utility, and make your widget's firmware outputGIDEIcommands and data keystrokes to type your data directly into Excel(though its probably safer to open a new Notepad with:Code:<esc>, combine, ctrl, escape.notepad<esc>enter.and have it type into that for the user to manually copy/paste into Excel).I'd recommend getting aCJMCU-232HFTDIFT232HUSB <=> serial breakout board. That particular chip can do async (UART) serial, SPI, I2C and also JTAG, and is easy to use from a variety of languages including Python on the PC (or Pi).Another option if the widget isn't portable and is far from the PC would be to have it write the log to some sort of memory that can be removed and read on the PC. Unfortunately the PIC16F1827 doesn't have enough RAM to write to a FAT formatted SD card, which removes the easy option of dumping the log to a CSV file on a SD card on demand, so you'd be looking at using some sort of EEPROM + a USB reader for it on the PC.

author: PerranOak
author_rank: Frequent Contributor
timestamp: April 25, 2021, 10:53:46 am
content: The temp logger is small and portable so no problem there.That looks like a great thing, thanks; I've ordered one!I can't wait until it arrives.

author: Ian.M
author_rank: Super Contributor
timestamp: April 25, 2021, 11:46:23 am
content: Adafruit's tutorial for their FT232H breakout board:https://learn.adafruit.com/adafruit-ft232h-breakoutwill help you get started with the CJMCU-232H. Just watch out for the minor differences in pinout - follow the silkscreen on the board, not Adafruit's pretty pictures! For I2C treat it as the older version of Adafruit's breakout and link D1 to D2, and also add external pullup resistors on SCL (D0) and SDA (D1+D2).N.B. The FT232H is 5V tolerant on its GPIO/MSSP pins, but outputs a 3.3V logic '1' level. As it lacks the VCCIO pin of older FTDI chips, external level shifting is required when interfacing to chips with lower Vcc than 3.3V. 3.3V logic '1' may not be sufficient for many MCUs and peripheral chips with standard CMOS input thresholds when running at 5V Vcc, so output pins will need an upwards level-shifting buffer which can be as simple as a non-inverting 74HCT gate. For I2C use the classic PhilipsAN97055MOSFET level shifting circuit.

author: PerranOak
author_rank: Frequent Contributor
timestamp: April 25, 2021, 02:01:20 pm
content: That's excellent, just what I need.For the 5V situation, could I use the 74LS08 (quad, 2-input .AND.), that I already have, with one input tied high?

author: Ian.M
author_rank: Super Contributor
timestamp: April 25, 2021, 05:02:52 pm
content: 74LS logic itself doesn't reliably output 5V CMOS levels. It typically only reaches 3.4V, OTOH on the input side 3.3V levels are fine. You can *try* your 74LS gate with a 1K pullup on the output to drag it higher, but don't be surprised if the result isn't clean enough for clock signals (e.g. if using the FT232H for SPI).74HCT CMOS logic, with 5V Vcc, has TTL compatible input levels but full 5V swing CMOS output levels.

author: SuntUnMorcov
author_rank: Contributor
timestamp: April 28, 2021, 08:43:18 pm
content: QuoteI'd recommend writing a simple application to receive, timestamp (unless the temperature scanner timestamps the data already), and record the data on the host PC. You'll be able to do this in practically any programming language you so desire.I'd avoid using anything like GIDEI (unless you *really* want to do that as a learning experience). Instead, I'd just define a very simple arbitrary format for streaming the measurement data (and any metadata, like a rolling unsigned 32-bit scan counter, etc) from your temperature scanner device to the host PC. There are plenty of measurement instruments out there that do this kind of thing already to draw inspiration from.If you record the measurement data using a simple and widely-supported file format (e.g., Comma-Separated Value file, etc), then you'll likely be able to import/use the recorded data easily with a range of applications and tools (e.g., Excel, Matlab, gnuplot, etc). If you want to 'beatify' and plot this data in Excel, then you can write some simple VBA macros to pull in the data and do whatever you require within an Excel workbook.Not only will this approach meet the basic requirements stated, you'll also end up with a solution that provides a greater degree of flexibility without much to any additional effort. I'd even wager this approach is more simple and less error prone.

author: Ian.M
author_rank: Super Contributor
timestamp: April 29, 2021, 03:38:49 am
content: I'd also avoid GIDEI unless you need to be able to fake hitting keyboard keys from a MCU with a UART but no direct USB interface, e.g. most low-end Arduinos. Another option I forgot to mention is use a terminal program, and copy/paste into Excel from it.