Lesen vom Fotowiderstandsmodul¶
In diesem Projekt erfassen wir die Lichtintensität und zeigen sie auf dem I2C LCD1602 an.
Schritte
In diesem Projekt wird ein I2C LCD1602 verwendet, daher ist es notwendig, die relevanten Bibliotheken herunterzuladen, damit es funktioniert.
cd ~/ wget https://github.com/sunfounder/raphael-kit/blob/master/python/LCD1602.py
Installieren Sie
smbus2
für I2C.sudo pip3 install smbus2
Speichern Sie den folgenden Code auf Ihrem Raspberry Pi und geben Sie ihm einen Namen, zum Beispiel
photoresistor.ty
.from robot_hat import ADC import LCD1602 import time # Create an ADC object to read the value from the photoresistor a0 = ADC(0) def setup(): # Initialize the LCD1602 LCD1602.init(0x27, 1) time.sleep(2) def destroy(): # Clear the LCD display LCD1602.clear() def loop(): while True: # Read the value from the photoresistor value0 = a0.read() # Display the read value on the LCD LCD1602.write(0, 0, 'Value: %d ' % value0) # Reduce the refresh rate to update once per second time.sleep(0.2) if __name__ == '__main__': setup() try: loop() except KeyboardInterrupt: destroy() except Exception as e: # Clear the LCD and print error message in case of an exception destroy() print("Error:", e)
Verwenden Sie den Befehl
sudo python3 photoresistor.ty
, um diesen Code auszuführen.