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
Related
I am having some problems reading analog values from my Arduino Mega using pyfirmata.
I use Arduino Mega with a Mega Sensor Shield.
I would like to read analog values from a HW-201 IR sensor (pin A5).
I have uploaded the Standard firmata sketch on Arduino IDE, and I am running the following code using Anaconda Spyder:
import serial
import serial.tools.list_ports
from pyfirmata import ArduinoMega, util
from time import sleep
def readArduinoPort():
COM = []
ports = list(serial.tools.list_ports.comports())
for p in ports:
if "Arduino" in p.description:
COM.append(str(p.device))
return COM
COM = readArduinoPort()
board = ArduinoMega(COM[0])
pin = board.get_pin('a:5:i')
it = util.Iterator(board)
it.start()
try:
while True:
print(pin.read())
sleep(0.1)
except KeyboardInterrupt:
pass
Now, this code runs just fine the first time I use it, generating numbers close to 1 when the sensor is detecting proximity and numbers close to 0 when not detecting anything.
However, whenever I try to run the Iterator a second time (without restarting the kernel), the code generates seemingly random numbers (almost like the pin was floating, like nothing was connected to it).
Any idea why this is happening? Is this a normal behavior?
Thank you!
I already checked these questions that do not entirely address my issue:
arduino pyfirmata analog reading
Analog readings on Arduino returns wrong values
I have the same result by not having anything pinned to pin A5 of the Arduino Mega.
I wasn't sure, but I tried adding:
board.analog[5].enable_reporting()
before the iterator and then:
board.analog[5].disable_reporting()
but nothing changed.
Board Differences
I ran into the same problem. I own both a Mega and an Uno and needed to select the appropriate Board and Processor in the tools menu:
Tools > Board > _board_
Tools > Processor > _processor_
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
At the moment I am trying to let two MCP3008's communicate through SPI with my raspberry pi and a Python script. A potentiometer should send a certain analog value to the MCP3008 input channel.
Here is my setup in Fritzing:
Breadboard Schematic
and here is the schematic overview:
Schematic Overview
The SPI wiring is based upon a standard daisychain schematic as shown in:
SPI Daisy Chain
The Python Code I am using is:
import spidev
import time
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 1000000
def read_spi(channel):
spidata = spi.xfer2([0,(8+channel)<<4,0])
return ((spidata[1] & 3) << 8) + spidata[2]
try:
while True:
channeldata = read_spi(0)
print (channeldata)
time.sleep(.1)
except KeyboardInterrupt:
spi.close()
I am getting values, but they fluctuate a lot with every value possible between 0 and 1023.
I've tried it with one MCP3008 without daisychaining of course and it worked fine, so my guess is that it has something to do with either the daisychain being incorrect, or the addressing of the MCP3008.
Can you guys help me out? Thanks alot!!
Cheers,
Devatu
Might be a late response, but according to the data sheet this chip can't be daisy-chained at all.
I am also looking for an ADC which can be daisy-chained.
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);
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 ?