DIN, DOUT, CLK, CS) du MCP3008 aux pins correspondantes sur le GPIO du Raspberry Pi pour communication SPI.Python
import spidev import time # Ouvre la connexion SPI sur le bus 0, device 0 spi = spidev.SpiDev() spi.open(0, 0) spi.max_speed_hz = 1000000 def read_channel(channel): adc = spi.xfer2([1, (8 + channel) << 4, 0]) data = ((adc[1] & 3) << 8) + adc[2] return data channel = 0 # Remplacer par le numéro de canal connecté au module RMS to DC try: while True: value = read_channel(channel) print(f"Valeur lue: {value}") time.sleep(1) except KeyboardInterrupt: spi.close()
spidev sur le Raspberry Pi pour gérer les communications SPI.sudo raspi-config.Interfacing Options > I2C et sélectionnez Yes pour activer l'interface I2C.Shell
sudo apt-get update sudo apt-get install -y i2c-tools python-smbus
Shell
sudo i2cdetect -y 1
Python
import smbus import time I2C_ADDR = 0xXX # Remplacez 0xXX par l'adresse I2C du wattmètre détectée antérieurement bus = smbus.SMBus(1) # 1 indique le canal I2C utilisé (/dev/i2c-1) while True: # Lecture d'un registre hypothétique; utilisez la documentation du wattmètre pour les commandes exactes data = bus.read_i2c_block_data(I2C_ADDR, 0x00, 2) print("Valeur lue:", data) time.sleep(1)
Python
import spidev import time # Créer une instance SPI spi = spidev.SpiDev() spi.open(0, 0) # (bus, device) spi.max_speed_hz = 1000000 def read_channel(channel): val = spi.xfer2([1, (8 + channel) << 4, 0]) data = ((val[1] & 3) << 8) + val[2] return data try: while True: value = read_channel(0) # Lire CH0 print("Valeur du Canal 0:", value) time.sleep(0.5) except KeyboardInterrupt: spi.close()
spidev sur votre Raspberry Pi pour permettre la communication SPI.MCU) is part of your project. Here's how you would generally make the connection for the MCP3008's MISO pin to a microcontroller:IC1:VCC.IC1:GND.IC1:IN.IC1:OUT. Ce point peut être connecté à un dispositif d'affichage ou de mesure pour observer la valeur DC obtenue.IC1:OUT et GND. Supposons l'utilisation d'un condensateur de 1μF pour ce filtrage.IC1:VCC to your positive power supply (+5V).IC1:GND to the ground (GND).IC1:IN.IC1:OUT and GND for output filtering.Thin Peach X-Wing
olfa
copilot