Probleme raspberry pi4 i2c et servo

Bonjour à tous j’ai un gros problème en fait je souhaite afficher les informations du capteur bme sur un écran oled et activer la pompe si la valeur du capteur d’humidité est égale à 0 lorsque je lance ce code tout fonctionne parfaitement :

import RPi.GPIO as GPIO
import time
import smbus2
import bme280
from datetime import datetime
from PIL import Image, ImageDraw
from luma.core.interface.serial import i2c
from luma.oled.device import sh1106

GPIO.setmode(GPIO.BCM)
pump_pin = 27
moisture_sensor_pin = 22

GPIO.setup(pump_pin, GPIO.OUT)
GPIO.setup(moisture_sensor_pin, GPIO.IN)

port = 1
address = 0x76
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)

serial = i2c(port=1, address=0x3C)
device = sh1106(serial)

def display_on_oled(data, moisture_value, current_time):
    image = Image.new("1", (device.width, device.height), "white")
    draw = ImageDraw.Draw(image)

    draw.text((5, 5), f"Temp: {data.temperature:.2f} C", fill="black")
    draw.text((5, 15), f"Pressure: {data.pressure:.2f} hPa", fill="black")
    draw.text((5, 25), f"Humidity: {data.humidity:.2f}%", fill="black")

    draw.line([(0, 35), (device.width, 35)], fill="black", width=1)
    draw.text((5, 40), f"Soil Moisture: {moisture_value}", fill="black")
    draw.line([(0, 50), (device.width, 50)], fill="black", width=1)
    draw.text((5, 55), f"Date: {current_time}", fill="black")

    device.display(image)

try:
    while True:
        moisture_value = GPIO.input(moisture_sensor_pin)
        data = bme280.sample(bus, address, calibration_params)
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        display_on_oled(data, moisture_value, current_time)

        if moisture_value == 0:

            GPIO.output(pump_pin, GPIO.HIGH)
            print("Pump activated.")
            time.sleep(5)

            GPIO.output(pump_pin, GPIO.LOW)
            print("Pump deactivated.")

        time.sleep(2)

except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()
    device.cleanup()

Mais quand j’ajoute le servo moteur il ne fonctionne plus et ça me donne une erreur que je ne comprends vraiment pas voici le code :


import RPi.GPIO as GPIO
import time
import smbus2
import bme280
from datetime import datetime
from PIL import Image, ImageDraw
from luma.core.interface.serial import i2c
from luma.oled.device import sh1106

GPIO.setmode(GPIO.BCM)
pump_pin = 27
moisture_sensor_pin = 22
SERVO_PIN = 13

GPIO.setup(pump_pin, GPIO.OUT)
GPIO.setup(moisture_sensor_pin, GPIO.IN)
GPIO.setup(SERVO_PIN, GPIO.OUT)

p = GPIO.PWM(SERVO_PIN, 50)  # Modifié la fréquence du PWM à 50 Hz
p.start(2.5)

port = 1
address = 0x76
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)

serial = i2c(port=1, address=0x3C)
device = sh1106(serial)

def display_on_oled(data, moisture_value, current_time):
    image = Image.new("1", (device.width, device.height), "white")
    draw = ImageDraw.Draw(image)

    draw.text((5, 5), f"Temp: {data.temperature:.2f} C", fill="black")
    draw.text((5, 15), f"Pressure: {data.pressure:.2f} hPa", fill="black")
    draw.text((5, 25), f"Humidity: {data.humidity:.2f}%", fill="black")

    device.display(image)

try:
    while True:
        moisture_value = GPIO.input(moisture_sensor_pin)
        data = bme280.sample(bus, address, calibration_params)
        current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        display_on_oled(data, moisture_value, current_time)
        
        temperature = data.temperature  # Ajout de cette ligne pour obtenir la température actuelle

        if temperature > 18:
            p.ChangeDutyCycle(2.5)
            time.sleep(30)
            
        else:
            p.ChangeDutyCycle(7)
            time.sleep(30)
            
        if moisture_value == 0:
            GPIO.output(pump_pin, GPIO.HIGH)
            print("Pump activated.")
            time.sleep(5)
            GPIO.output(pump_pin, GPIO.LOW)
            print("Pump deactivated.")

        time.sleep(2)

except KeyboardInterrupt:
    pass

finally:
    p.stop()  # Arrêter le PWM
    GPIO.cleanup()
    device.cleanup()

Voici l’erreur qu’il me donne :

^C(myvenv) ysk@raspberrypi:~ $ python forum.py
Traceback (most recent call last):
File « /home/ysk/forum.py », line 28, in
device = sh1106(serial)
^^^^^^^^^^^^^^
File « /home/ysk/myvenv/lib/python3.11/site-packages/luma/oled/device/init.py », line 96, in init
self.clear()
File « /home/ysk/myvenv/lib/python3.11/site-packages/luma/core/mixin.py », line 46, in clear
self.display(Image.new(self.mode, self.size))
File « /home/ysk/myvenv/lib/python3.11/site-packages/luma/oled/device/init.py », line 133, in display
self.data(list(buf))
File « /home/ysk/myvenv/lib/python3.11/site-packages/luma/core/device.py », line 55, in data
self._serial_interface.data(data)
File « /home/ysk/myvenv/lib/python3.11/site-packages/luma/core/interface/serial.py », line 128, in data
write(list(data[i:i + block_size]))
File « /home/ysk/myvenv/lib/python3.11/site-packages/luma/core/interface/serial.py », line 137, in _write_large_block
self._bus.i2c_rdwr(self._i2c_msg_write(self._addr, [self._data_mode] + data))
File « /home/ysk/myvenv/lib/python3.11/site-packages/smbus2/smbus2.py », line 658, in i2c_rdwr
ioctl(self.fd, I2C_RDWR, ioctl_data)
OSError: [Errno 5] Input/output error
SVP j’ai vraiment besoin d’aide

Bonjour,

Il faut aller dans raspi-config et vérifier que l’I2C est activé.

A+