📜 ⬆️ ⬇️

Expansion card for modeling on Raspberry Pi

A long time ago, the Raspberry Pi microcomputer entered the life of geeks, system administrators, programmers and electronics engineers. Inexpensive and relatively powerful for its class, with built-in I / O ports, it can handle various tasks and meet the needs of the user. Having bought a Raspberry Pi, I wanted to include, measure, manage external devices. At the moment, a large number of expansion cards are sold, for example, as here , you can use the Breadboard with wires for quick prototyping, but I prefer to make the devices myself, for specific tasks. For the first time, I didn’t use a double-row comb for all the outputs, but limited myself to several I / O ports, an SPI, I2C and UART bus. Connect Raspberry Pi with target wires for prototyping "mother-mother."

image

In this regard, a series of three prototyping boards was developed; I will tell you about the simplest one in this article.
')
So, what was necessary for me:


According to the above list, an electrical circuit was developed:
image
Scheme from high-resolution storage

The circuit uses a transformer power supply with two output windings. The first winding works on a linear voltage regulator with an output of 5V, for powering the RS485 driver chip and a real-time clock. The second winding is used to power the electromagnetic relays and assembly with Darlington transistors.

The DS18B20 temperature sensor is powered by + 3.3V from the Raspberry Pi board, there is a connector on the board for connecting external DS18B20 sensors, the same voltage is taken for the ADC and for matching the levels with the real-time clock. In the scheme, four buttons S1-S4 are used to control any actions that operate on a low logic level. To control the load transistor assembly is used DD1 ULN2003 with built-in protective diodes. Relays K1 and K2 are connected to pins 16, 15 of the transistor assembly, LEDs for indication are connected to pins 14-12, pins 11, 10 are intended for connection to external devices according to the scheme with a common collector or additional relays with a winding voltage of + 12V.

To measure the voltage, two channels of 10 bit ADC DD3 MCP3008 with SPI interface are used, with an input low-pass filter. Analog node is made primitively, for academic purposes or quick debugging. If the question arises about the qualitative measurement of the analog signal, it is necessary to untie the digital and analog ground, use an external source of the reference voltage. Alternatively, the analog part can be done like this:



You only need to use the TTL → LVTTL level converter on the SPI bus.

The real-time clock DD4 is made on the DS1307 microcircuit; the Q1 crystal at 32.768 KHz is used for clocking. To power the RAM of the microcircuit of the clock, a 3-volt lithium battery soldered into the board is used. The microcircuit is connected via a 5V-3.3V level converter made on VT2, VT3 MOSFETs to Raspberry Pi via the I2C bus (In CAD, the connection to the lines without explicit communication was used).

As a driver UART → RS485, the chip DD2 ST485 is used. Circuit solutions using the transistor VT1 allowed to abandon a separate output to control the transceiver. It switches to the transfer mode only when the UART transmits data, the rest of the time ST485 is in receive mode. The control command transceiver is removed from the collector of the transistor VT1.

In addition to interface conversion, this scheme also performs the function of matching the LVTTL levels of the UART Raspberry Pi interface with the TTL levels of the ST485 driver. The signal taken from the collector of the transistor VT1 tightens the level at the DI input to 5V, and the resistor R16 and the zener diode VD8 limit the level from the output R0 to 3.3V. Do not pay attention to the resistor R11, it remained in the circuit when debugging. RS485 is tested with the ModBus RTU protocol at a speed of 115200 baud.

Accordingly, a printed circuit board was further developed:



3D models of the PCB



Photos of the finished device and work with the expansion card:







Variant of connection scheme with external devices:


To check the nodes of the expansion card, I used a couple of scripts in the python language (I don’t know this language, I wrote it by trial and error, while peeping at specialists' codes)
To measure the voltage from the potentiometer connected to the ADC channels, I wrote a simple script:

MCP3008 python source
#!/usr/bin/python # Example program to read data from MCP3008 10 bit ADC import spidev import time import os # Open SPI bus spi = spidev.SpiDev() spi.open(0,0) # Function to read SPI data from MCP3008 chip # Channel must be an integer 0-7 def ReadChannel(channel): adc = spi.xfer2([1,(8+channel)<<4,0]) data = ((adc[1]&3) << 8) + adc[2] return data # Function to convert data to voltage level, # rounded to specified number of decimal places. def ConvertVolts(data,places): volts = (data * 3.3) / float(1023) volts = round(volts,places) return volts # Define sensor channels first_channel = 0 second_channel = 1 # Define delay between readings delay = 5 print "------------------------------------------------------" while True: # Read the first channel data first_level = ReadChannel(first_channel) first_channel_volts = ConvertVolts(first_level,2) # Read the second channel data second_level = ReadChannel(second_channel) second_channel_volts = ConvertVolts(second_level,2) # Print out results print "------------------------------------------------------" print("First ADC channel: {} ({}V)".format(first_level,first_channel_volts)) print("Second ADC channel : {} ({}V)".format(second_level,second_channel_volts)) # Wait before repeating loop time.sleep(delay) 



The following script was used to work with DS18B20 thermometers:

DS18B20 python source
Code is used from the site
 # Example program to read data from DS18B20 # This code taken from # https://kropochev.com/?go=all/raspberry-pi-and-onewire-sensor/ import os import glob import time os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '10*')[0] device_file = device_folder + '/w1_slave' def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0 temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c, temp_f while True: print(read_temp()) time.sleep(1) 


I also documented the settings in Raspbian:

Configure devices
- 1 Wire settings:
- Raspbian wheezy

Enter command in console:
pi @ raspberrypi ~ $ sudo modprobe w1-gpio
pi @ raspberrypi ~ $ sudo modprobe w1_therm

Then check sensor in system:
pi @ raspberrypi ~ $ sudo ls / sys / bus / w1 / devices / w1_bus_master1 /

You can see the tabel below comman and
there you should find HEX like 28-000002da8328;
This is DS18B20 address;

Next read the data from DS18B20 sensor using command:
pi @ raspberrypi ~ $ cat / sys / bus / w1 / devices / w1_bus_master1 / 28-000002da8328 / w1_sla

Like this:
6f 01 4b 46 7f ff 01 10 67: crc = 67 YES
6f 01 4b 46 7f ff 01 10 67 t = 22937

t = 22937 - you should divide this number in 1000 and you will have temperature in Celsius;

- Raspbian Jezzy

Enter command:
pi @ raspberrypi ~ $ sudo nano /boot/config.txt

On next step you should write in config file
dtoverlay = w1-gpio, gpiopin = 4
dtoverlay = w1-gpio-pullup

Then you should not reboot your raspberry Pi;

- I2C Real Time Clock settings:
- Update your system if it needed and install i2c-tools:
pi @ raspberrypi ~ $ sudo apt-get update
pi @ raspberrypi ~ $ sudo apt-get -y upgrade
pi @ raspberrypi ~ $ sudo apt-get i2c-tools:

Enter command:
pi @ raspberrypi ~ $ sudo nano / etc / modules
Add these lines:
i2c-bcm2708
i2c-dev
rtc_ds1307

Comment one line in file:
pi @ raspberrypi ~ $ sudo nano /etc/modprobe.d/raspi-blacklist.conf
Add # Symbol in beginning of line

blacklist i2c-bcm2708
________________________________________________________
Reboot system;
Enter command:
pi @ raspberrypi ~ $ sudo lsmod

You will see lines like:
rtc_ds1307 7715 0
i2c_dev 5277 0
i2c_bcm2708 4719 0
________________________________________________________

Get DS1307 address:
pi @ raspberrypi ~ $ sudo i2cdetect-y 1

You will see table in console:

0 1 2 3 4 5 6 7 8 9 abcdef
00: - - - - - - - - - - - - - 10: - - - - - - - - - - - - - - - -
20: - -- - -- - -- - -- - -- - -- - -- - --
30: - - - - - - - - - - - UU - - - -
40: - - - - - - - - - - - - - - - -
50: - -- - -- - -- - -- - -- - -- - -- - --
60: - - - - - - - - 68 - - - - - - - 70: - - - - - - - -

In this case, the DS1307 clock address is 0x3b.

Enter command:
echo ds1307 0x68> / sys / class / i2c-adapter / i2c-1 / new_device

Read clock:
pi @ raspberrypi ~ $ sudo hwclock -r

Set time:
pi @ raspberrypi ~ $ sudo hwclock -w
Set system time from RTC:
pi @ raspberrypi ~ $ sudo hwclock -s

Automatic RTC start;
Add lines in /etc/rc.local file

echo ds1307 0x68> / sys / class / i2c-adapter / i2c-1 / new_device
sudo hwclock -s
Before last line in file looks like:

exit 0
- Uart settings:
- Back up files:
cp /boot/cmdline.txt /boot/cmdline.bak
cp / etc / inittab /etc/inittab.bak

Delete "console = ttyAMA0,115200" and "kgdboc = ttyAMA0,115200" lines from configuration file:
pi @ raspberrypi ~ $ nano /boot/cmdline.txt

Comment last line looks like "T0: 23: respawn: / sbin / getty -L ttyAMA0 115200 vt100: in / etc / inittab file: using # symbol:
pi @ raspberrypi ~ $ nano / etc / inittab


The printed circuit board was needed urgently, so I made it myself using LUT technology and a couple of hours of free time. In order to facilitate the work and save time, the PCB made one-sided with jumpers from MGSHV 0.5. DipTrace circuit and circuit design, test source codes, component list and tutorial with commands for configuration in
repositories

PS: in the video below, the very first board is used for modeling, it was assembled on a breadboard with buttons and LEDs for 20 minutes, the following versions appeared on the basis of it:

Source: https://habr.com/ru/post/401089/


All Articles