I am trying to use MCP4728 DAC with RP4.
I can’t get it to work.
Connection map:
RP4 1 (3.3V) to 1 at 4728.
RP4 6 (GND) to 10 at 4728.
RP4 3 (SDA) to 3 at 4728.
RP4 5 (SCL) to 2 at 4728.
GND to 4 at 4728.
Tried using this code:
import board
import busio
import adafruit_mcp4728
i2c = busio.I2C(board.SCL, board.SDA)
mcp4728 = adafruit_mcp4728.MCP4728(i2c)
with an error: No I2C device at address: 0x60
Tried using this code:
from smbus import SMBus
bus = SMBus(1) #indicates /dev/i2c-1
addr = 0x60
bus.write_byte(addr, 0x50)
with an error: [Errno 121] Remote I/O error
Any ideas ?
Thank you.
This worked for me.
# write to MCP4728
#for the first channel
command = 0x58
addr = 0x61 #bus address
bus = SMBus(1) #indicates /dev/i2c-1
bus.write_i2c_block_data(addr,command,[value_msb,value_lsb])
Related
I want to set up my serial connection to a pump with a USB to 4xRS232 (FT4232H chip). However, my serial connection can't write output from the pump. The whole setup works with a single usb to rs232 converter but not with my USB to 4xRS232 converter. My raspberry recognizes all 4 USB ports:
/dev/ttyAMA0: ttyAMA0 [fe201000.serial]
/dev/ttyUSB1: FT4232H Device
/dev/ttyUSB2: FT4232H Device
/dev/ttyUSB3: FT4232H Device
/dev/ttyUSB4: FT4232H Device
My code to write and read information with my external device looks as following:
import serial
import time
global ser
ser = serial.Serial()
ser.port = '/dev/ttyUSB1'
#ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.open()
encoding='utf-8'
def pump_loop():
if ser.inWaiting() == 0:
out_press=''
ser.write(b'PRESSURE?\r')
time.sleep(.1)
#print(ser.in_waiting)
#print(ser.read())
while int(ser.in_waiting) > 0:
out_press += str(ser.read(1), encoding)
print(out_press)
pump_loop()
Edit: Cable set-up is the following: raspberry-usb--> FT4232H --> 4 RS232 --> female-to-female coupling --> pump
Closed: had to swap PIN 2 and 3 before using the female-female coupler, as they are swapped inside.
I'm having problem with minimalmodbus library. The slave does not respond to the master's request, I want to request a read. I'm using the Raspberry Pi 3 Model B+, with Python 3.10.1, it's the minimalmodbus library with version 2.0. I'm using the Arduino Mega as a slave and I'm also using a Mini Adapter Serial Converter USB to RS485 is a Converter Module RS485 for Arduino.
import serial
import minimalmodbus
instrument = minimalmodbus.Instrument('COM6',1)
instrument.serial.baudrate = 9600
instrument.serial.timeout = 10
instrument.clear_buffers_before_each_transaction = True
instrument.debug = True
temperature = instrument.read_register(1,1)
print(temperature)
MinimalModbus debug mode. Will write to instrument (expecting 7 bytes back): 01 03 00 01 00 01 D5 CA (8 bytes)
MinimalModbus debug mode. Clearing serial buffers for port COM6
MinimalModbus debug mode. No sleep required before write. Time since previous read: 87898406.00 ms, minimum silent period: 4.01 ms.
MinimalModbus debug mode. Response from instrument: (0 bytes), roundtrip time:10.0 ms. Timeout for reading: 0 ms.
I have a similar issue on Win10 python 3.7.9. When I write
import minimalmodbus
instrument = minimalmodbus.Instrument('COM3', 2)
instrument.serial.baudrate = 9600
instrument.clear_buffers_before_each_transaction = True
reg_0 = instrument.read_register(0, 0)
reg_1 = instrument.read_register(1, 0)
print(reg_0)
print(reg_1)
instrument.serial.close()
I have error message "minimalmodbus.NoResponseError: No communication with the instrument (no answer)". But, when I write
import minimalmodbus
instrument = minimalmodbus.Instrument('COM3', 2)
instrument.serial.baudrate = 9600
instrument.clear_buffers_before_each_transaction = True
instrument.debug = True
reg_0 = instrument.read_register(0, 0)
reg_1 = instrument.read_register(1, 0)
print(reg_0)
print(reg_1)
instrument.serial.close()
all works correct.
I use Arduino UNO as a slave. In ModbusPoll both registrers read correct
Upd. When I use construction "try... except..." all works correctly
import minimalmodbus
instrument = minimalmodbus.Instrument('COM3', 2)
instrument.serial.baudrate = 9600
instrument.clear_buffers_before_each_transaction = True
try:
reg_0 = instrument.read_register(0, 0)
except minimalmodbus.NoResponseError:
reg_0 = instrument.read_register(0, 0)
reg_1 = instrument.read_register(1, 0)
print(reg_0)
print(reg_1)
instrument.serial.close()
Sorry for my bad English.
I am trying to read data by the Modbus RTU method (library: minimalmodbus) but have a problem.
This is my 'Modbus Poll' display.
I would like to read data by using minimalmodbus.
import minimalmodbus
import serial
instrument = minimalmodbus.Instrument('COM5', 1) # port name, slave address (in decimal)
instrument.serial.port = 'COM5' # this is the serial port name
instrument.serial.baudrate = 9600 # Baud
instrument.serial.bytesize = 8
instrument.serial.parity = serial.PARITY_NONE
instrument.serial.stopbits = 1
instrument.serial.timeout = 0.1 # seconds
instrument.address = 1 # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
result = instrument.read_float(0, 4, 20)
print(result)
But, I keep failing.
Can please someone help me with how to read data?
So I'm doing a project where I want my Raspberry Pi 4 to communicate through Modbus. I've bought a shield to enable RS485 communication from the Pi and I've been modifying the demo code (Software/Test Codes/MODBUS/rtumaster.py) and have been using a slave simulator on my computer to test.
I'm reaching out because I've gotten stuck on trying to read data from my slave. The command seems to go through okay (or the slave simulator doesn't complain is perhaps better to say), but I don't know how to get the data into my program. I've been trying something like this:
read = logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 5, 1))
And then trying to print that value to check, but it just returns 'None' instead of my value.
Any help is appreciated.
Current code:
## To install dependencies:
## sudo pip3 install modbus-tk
##################################################################################################
import serial
import fcntl
import os
import struct
import termios
import array
#import modbus lib
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus as modbus
#import modbus_tk.modbus_rtu as modbus_rtu
from modbus_tk import modbus_rtu
# RS485 ioctls define
TIOCGRS485 = 0x542E
TIOCSRS485 = 0x542F
SER_RS485_ENABLED = 0b00000001
SER_RS485_RTS_ON_SEND = 0b00000010
SER_RS485_RTS_AFTER_SEND = 0b00000100
SER_RS485_RX_DURING_TX = 0b00010000
# rs 485 port
ser1 = serial.Serial("/dev/ttySC0",19200)
ser2 = serial.Serial("/dev/ttySC1",9600)
def rs485_enable():
buf = array.array('i', [0] * 8) # flags, delaytx, delayrx, padding
#enable 485 chanel 1
fcntl.ioctl(ser1, TIOCGRS485, buf)
buf[0] |= SER_RS485_ENABLED|SER_RS485_RTS_AFTER_SEND
buf[1] = 0
buf[2] = 0
fcntl.ioctl(ser1, TIOCSRS485, buf)
#enable 485 chanel 2
fcntl.ioctl(ser2, TIOCGRS485, buf)
buf[0] |= SER_RS485_ENABLED|SER_RS485_RTS_AFTER_SEND
buf[1] = 0
buf[2] = 0
fcntl.ioctl(ser2, TIOCSRS485, buf)
#end of rs485_enable():
if __name__ == '__main__':
logger = modbus_tk.utils.create_logger("console")
rs485_enable()
#set modbus master
master = modbus_rtu.RtuMaster(
serial.Serial(port= '/dev/ttySC0',
baudrate=9600,
bytesize=8,
parity='N',
stopbits=1,
xonxoff=0)
)
master.set_timeout(5.0)
master.set_verbose(True)
logger.info("connected")
read = logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 5, 1))
print("Value is: ", read)
I'm trying to interface my accelerometer ADXL3458 to my Raspberry Pi 3 running in ubuntu mate. I have install all the necessary package require for the I2C communication. When I perform this command i2cdetect -y 1 I get this results.
Now I run this Python code
#!/usr/bin/env python
import smbus
import time
import math
from math import sin, cos, pi
bus = smbus.SMBus(1)
print bus
ACC_ADRESS = 0x53
acc_x = 0.0
acc_y = 0.0
acc_z = 0.0
def writeACC (register, value):
bus.write_byte_data(ACC_ADRESS, register, value)
return -1
def readACC_byte ( addr):
return bus.read_byte_data(ACC_ADRESS, addr)
def readACC_word (addr):
LSB = bus.read_byte_data(ACC_ADRESS, addr)
MSB = bus.read_byte_data(ACC_ADRESS, addr + 1)
val = (MSB << 8) | LSB
return val
def setupACC ():
# Sleep mode
writeACC(0x2D, 0)
# Mesurement mode
writeACC(0x2D, 8)
# enable Autu sleep mode
writeACC(0x2D, 16)
while True:
time.sleep(0.1)
acc_x = readACC_word(0x32)
acc_y = readACC_word(0x34)
acc_z = readACC_word(0x36)
print "Acc_x :\n", acc_x
print "Acc_y :\n", acc_y
print "Acc_z :\n", acc_z
time.sleep(0.5)
if __name__ == '__main__':
setupACC()
And I get this result in the oscilloscope
This infer that my I2C communication is successful. But in the result that I'm printing shows no value in it
Can please help with problem that I'm facing. Is it anything that I'm doing wrong?
thank you
Auto sleep mode was should not active. Upon commenting
#writeACC(0x2D, 16)
is working fine.