smbus2 write_i2c_block_data: ¿qué es el byte "cmd" para un ADS1115?
Traducido del English por IA ·
Proyecto de nodo de jardín: Pi Zero 2 W, ADS1115 en 0x48, AIN0 es una sonda capacitiva de humedad del suelo y AIN1 la batería de 12 V a través de un divisor. El script envía ambos valores a Hermetry cada 5 minutos.Quería evitar las bibliotecas grandes y comunicarme con el ADS1115 con smbus2 directamente. Copié esto de una publicación del foro:
bus.write_i2c_block_data(0x48, 0x01, [0xC3, 0x83])
time.sleep(0.01)
data = bus.read_i2c_block_data(0x48, 0x00, 2)
raw = data[1] << 8 | data[0]
Funciona, pero los números no tienen sentido: el valor del suelo salta entre unos 900 y 52 000 aunque nada cambie. La documentación de smbus2 describe el segundo argumento como cmd. ¿Qué comando es? La hoja de datos del ADS1115 no menciona comandos en ninguna parte, solo registros. Creo que me falta algo básico.
Garden node project: Pi Zero 2 W, ADS1115 at 0x48, AIN0 is a capacitive soil probe and AIN1 the 12 V battery through a divider. The script posts both to Hermetry every 5 minutes.
I wanted to skip the big libraries and talk to the ADS1115 with smbus2 myself. I copied this from a forum post:
bus.write_i2c_block_data(0x48, 0x01, [0xC3, 0x83])
time.sleep(0.01)
data = bus.read_i2c_block_data(0x48, 0x00, 2)
raw = data[1] << 8 | data[0]
It runs, but the numbers are nonsense: the soil value jumps between about 900 and 52,000 when nothing changes. The smbus2 docs describe the second argument as cmd. What command? The ADS1115 datasheet does not mention commands anywhere, only registers. I feel like I’m missing something basic.
Traducido del English por IA ·
¿No es cmd un código que define el chip, como «iniciar conversión»?
Isn’t cmd a code the chip defines, like «start conversion»?
Traducido del English por IA ·
+1, nunca entendí ese argumento tampoco.
+1, never understood that argument either.
Traducido del English por IA ·
«cmd» es la nomenclatura de SMBus. En una escritura de bloque SMBus, el primer byte después de la dirección se llama byte de comando, y en chips basados en registros como el ADS1115 ese byte es el puntero de registro. Así que tu primera línea coloca el puntero en el registro Config (0x01) y escribe en él 0xC3 y luego 0x83. En el bus: start, 0x90, 0x01, 0xC3, 0x83, stop.0xC383 significa: iniciar una conversión única, AIN0 respecto a GND, rango de ±4,096 V, modo single-shot, 128 muestras/s. La lectura pone el puntero en 0x00 (registro de conversión) y lee dos bytes.Tu error está en el orden de los bytes. El ADS1115 envía primero el byte más significativo, y el resultado es con signo:
data = bus.read_i2c_block_data(0x48, 0x00, 2)
raw = (data[0] << 8) | data[1]
if raw > 0x7FFF:
raw -= 0x10000
volts = raw * 4.096 / 32768
Con los bytes intercambiados, un pequeño cambio en el byte bajo se convierte en un salto enorme, que es tu 900 frente a 52.000. Para AIN1 cambia los bits MUX: [0xD3, 0x83]. Y 10 ms son suficientes a 128 SPS (una conversión dura unos 8 ms), así que la pausa está bien.
«cmd» is SMBus naming. In an SMBus block write the first byte after the address is called the command byte, and for register-based chips like the ADS1115 that byte is the register pointer. So your first line puts the pointer on the Config register (0x01) and writes 0xC3 then 0x83 into it. On the wire: start, 0x90, 0x01, 0xC3, 0x83, stop.
0xC383 means: start a single conversion, AIN0 against GND, ±4.096 V range, single-shot mode, 128 samples/s. The read sets the pointer to 0x00 (Conversion register) and reads two bytes.
Your bug is the byte order. The ADS1115 sends the most significant byte first, and the result is signed:
data = bus.read_i2c_block_data(0x48, 0x00, 2)
raw = (data[0] << 8) | data[1]
if raw > 0x7FFF:
raw -= 0x10000
volts = raw * 4.096 / 32768
With the bytes swapped, a small change in the low byte becomes a huge jump, which is your 900 vs 52,000. For AIN1 change the MUX bits: [0xD3, 0x83]. And 10 ms is enough at 128 SPS (one conversion is about 8 ms), so the sleep is fine.
Traducido del English por IA ·
Consejo de las unidades de puesta en marcha: mientras pruebas, registra la cuenta bruta junto a los voltios. Cuando una lectura parezca incorrecta dentro de tres meses, podrás distinguir un error de ganancia de un problema del sensor directamente en el gráfico.
Tip from commissioning skids: while you’re testing, send the raw count next to the volts. When a reading looks wrong in three months you can tell a gain mistake from a sensor problem straight from the chart.
Traducido del English por IA ·
Era el orden de los bytes. Al intercambiarlos, el valor del suelo se queda estable en 1.62 V. Añadí soil.raw junto a soil.v como dijo Keisha, y la batería en AIN1 marca 12.7 V tras el cálculo del divisor. Además, ya entiendo qué es ese argumento, gracias Aoife.
Byte order it was. Swapped them and the soil value sits at 1.62 V, steady. I added soil.raw next to soil.v like Keisha said, and the battery on AIN1 reads 12.7 V after the divider maths. Also understand now what that argument is, thanks Aoife.
- Debes estar registrado para responder a este debate.
Únete a la conversación
Inicia sesión para preguntar y responder, dar me gusta a las publicaciones útiles y marcar la respuesta que resolvió tu problema. Cada aportación suma puntos y niveles.

