Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4347

Troubleshooting • i2C and servo problem raspberry pi 4

$
0
0
Hello everyone I have a big problem actually I want to display the information from the bme sensor on an oled screen and activate the pump if the value of the humidity sensor is equal to 0 when I run this code everything works perfectly:

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()


But when I add the servo motor it no longer works and it gives me an error I really don't understand here is the 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()

Here is the error it gives me:

^C(myvenv) ysk@raspberrypi:~ $ python forum.py
Traceback (most recent call last):
File "/home/ysk/forum.py", line 28, in <module>
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

Statistics: Posted by ysk05 — Sat Jan 20, 2024 4:09 pm — Replies 0 — Views 2



Viewing all articles
Browse latest Browse all 4347

Trending Articles