How can I get i2c values from a Rapsberry with Python? - python

I use an ads1115 to get values from an analogic sensor to a raspberry pi 3 but I'm having a hard time figuring out how to get them from python.
I use the SMBus library to get the i2c values but I can't find how to actually get the analog 0 AIN0 channel values. I found the i2c address for the ads1115 to be 0x48 but I can't find the address for the AIN0 channel, thus I don't have the second argument for the command smbus.read_byte_data(0x48, ???) and I tried some addresses like 0x00, 0x01 but it always gives me the same values even tho the sensor values should be changing.Here's my code :
from smbus import SMBus
import RPi.GPIO as GPIO
import time
def main():
i2cbus = SMBus(1)
i2caddress = 0x48
value = i2cbus.read_byte_data(0x48, """dont't know""")
if __name__ == "__main__":
main()

To read digital data from ADS1115 ADC IC, you must connect analog signals to A[0]~A[3] pins of ADS1115. After ADS1115 ADC module converts the analog signal on pin A[0]~A[3] to digital form, you should read the value through the I2C interface. Check out the references section to review the case study with the ADS1115 module.
The following command call reads 2 bytes of data starting from the 0x00 register address of the I2C device whose slave address is 0x48.
# read_i2c_block_data(i2c_address, register, length, force=None)
# i2c_address -> ADS1115 I2C slave address
# register -> ADS1115 conversion register address
# length -> desired block length
value = i2cbus.read_i2c_block_data(0x48, 0x00, 2)
References
ADS1115 ADC Module Example with SMBus
ADS1115 Python Driver

Related

raspberry python prints empty b ' ' from a serial port

I want to connect a pressure sensor that use a RS485 communication to raspberry pi,
when I connected it to RS485 to UART converter to the raspberry
but when I ran the code the output is b'' with no numbers from the sensor
can anybody know how I can read from the sensor ?
'KITA' is the sensor I use.
import serial
import time
ser = serial.Serial('/dev/serial0', 9600, timeout=0.1)'
ser.write(b'$PF,200\r\n')
ser.write(b'$PO,20\r\n')
x = ser.read()
print (x)

RaspberryPi 3 temperature sensor remote I/O error (python)

I have a temperature and humidity sensor connected to my RPi via 3.3v power, GND and SDA and SCL. I am trying to get a temperature reading from it but I keep getting a
"OSerror: [Errno 121] Remote I/O error".
The i2c interface is enabled and everything is up to date. I can see the address of the device using sudo i2cdetect -y 1. Could it be possible that i should be sending some data to it first so that the sensor "knows" that i am about to read from it and it should be sending data ?
This is my code
import smbus
TEMP = 0x40
bus = smbus.SMbus(1)
state = 1
while (state):
print("1.Read temperature")
print("2.Exit")
choice = input("")
if choice =='1':
Ctemp = bus.read_byte(TEMP)
print ("%d" %Ctemp)
elif choice == '2':
state = 0
Yes, it's possible you might have to configure your I2C device (you don't mention what you use, so I can't look that up for you) but this error probably means something else.
You can try i2cdump -y 1 0x40 to see what data is available, but again, you should read the sensor's chip's datasheet in order to know what to work with.

Controlling RGB-Matrix via SPI (Python)

The Raspberry Pi has a Broadcom BCM 2835 chip allowing it to interface with SPI devices on its GPIO pins. I encountered a problem adressing LEDs in a 8x8 Matrix via SPI.
I want to adress single LEDs on the matrix, but I haven't figured out, which bytes adresses which LED(s).
SPIdev seems to be the right library for this purpose und I stumbled upon this snippet of code:
import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 7629
# Split an integer input into a two byte array to send via SPI
def write_pot(input):
msb = input >> 8
lsb = input & 0xFF
spi.xfer([msb, lsb])
# Repeatedly switch a MCP4151 digital pot off then on
while True:
write_pot(0x1FF)
time.sleep(0.5)
write_pot(0x00)
time.sleep(0.5)
Does anyone know which values adresses (a) specific LED(s)?
In C/C++ adressing works very well via the wiringpi-library, a useful documentation can be found here:
http://wiki.52pi.com/index.php/RPI-RGB-LED-Matrix_SKU:EP-0075

How to use Arduino Uno analog pins as digital pins with pyfirmata

I am trying to use Arduino analog pins (A0-A5) as digital pins (D14-D19) with pyfirmata. I'm doing a simple blink test with D14 (or A0) to test this:
#!/usr/bin/python
# Blink test with analog as digital pins
# Import required libraries
from pyfirmata import Arduino, util
from pyfirmata import INPUT, OUTPUT, PWM
from time import sleep
# Associate port and board with pyFirmata
port = '/dev/cu.usbmodem1451'
board = Arduino(port)
# Pause to sync
sleep(1)
led = board.get_pin('d:14:o')
time = 1
while True:
led.write(1)
print ("On")
sleep(time)
led.write(0)
print ("Off")
sleep(time)
To enable analog pins to act as digital pins, I've gone into pyfirmata's boards.py file (as show in the github repo at https://github.com/tino/pyFirmata/blob/master/pyfirmata/boards.py) and changed line #3 from
digital': tuple(x for x in range(14)),
to
digital': tuple(x for x in range(20)),
I don't get an error when I run my python script, but the LED doesn't blink (don't worry, I'm positive the LED was in the right way).
Does anyone know how I can effectively instantiate digital pin 14 as an output pin with pyfirmata (as in the line led = board.get_pin('d:14:o'))
By the way (not that I think this affects anything) but I'm running Mac OSX with Yosemite.
Thank you!
The standard firmata that we use in Arduino, makes all analog pins input by default. So instead of changing anything in boards.py file, go to standard firmata file in arduino ide and find this ...
switch (mode) {
case PIN_MODE_ANALOG:
if (IS_PIN_ANALOG(pin)) {
if (IS_PIN_DIGITAL(pin)8) {
pinMode(PIN_TO_DIGITAL(pin), INPUT);
Now comment the pinMode line so that the firmata does not make any analog pin input
Now go to void setup() function and explicitly define nature of analog pins as shown below.
Firmata.begin(57600);
while (!Serial) {
}
pinMode(14,OUTPUT);
pinMode(15,INPUT);
pinMode(16,INPUT);
pinMode(17,INPUT);
pinMode(18,INPUT);
pinMode(19,INPUT);
digitalWrite(14,LOW);

LCD commands not working inside threads

I hooked up a 16X2 LCD to the pi via a MCP23017 GPIO expander. The problem that I am facing is that the LCD code below works inside normal functions or when I call it individually. But it does not work inside any of the threads in my program.
#!/usr/bin/python
# Example script to show usage of MCP230xx GPIO extender to drive character LCD.
from Adafruit_CharLCD import Adafruit_CharLCD
from Adafruit_MCP230xx import MCP230XX_GPIO
bus = 1 # Note you need to change the bus number to 0 if running on a revision 1 Raspberry Pi.
address = 0x20 # I2C address of the MCP230xx chip.
gpio_count = 8 # Number of GPIOs exposed by the MCP230xx chip, should be 8 or 16 depending on chip.
# Create MCP230xx GPIO adapter.
mcp = MCP230XX_GPIO(bus, address, gpio_count)
# Create LCD, passing in MCP GPIO adapter.
lcd = Adafruit_CharLCD(pin_rs=1, pin_e=2, pins_db=[3,4,5,6], GPIO=mcp)
lcd.clear()
lcd.message(" Adafruit 16x2\n Standard LCD")
links to library programs,
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_CharLCD/Adafruit_CharLCD.py
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_MCP230xx/Adafruit_MCP230xx.py
part of my actual program:
def switch():
mcp.config(0, mcp.INPUT)
mcp.pullup(1,1)
if ((mcp.input(1)>>1))
lcd.clear()
lcd.message("Switch open")
switchthread=threading.thread(target=switch)
switchthread.start()
Can anyone please tell me how to get the LCD commands working in the threads ?

Categories

Resources