#!/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)
# 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)
- 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
Source: https://habr.com/ru/post/401089/
All Articles