Thursday, February 2, 2023

Raspberry Pi Pico DIY Light Meter Micropython sensor BH1750 (GY-302)

Raspberry Pico project light meter Thonny Micropython

Light sensor BH1750 (GY-302) and OLED display SSD1306
I2C communication

Video link Thonny IDE
Test code: from machine import I2C, Pin from BH1750 import BH1750 from ssd1306 import SSD1306_I2C from time import sleep i2c = I2C(0,sda=Pin(16), scl=Pin(17), freq=400000) oled = SSD1306_I2C(128,64,i2c) ## Oled Display pixel size 128x64 oled.fill(0) light = BH1750(i2c) while True: lux=light.luminance(BH1750.ONCE_HIRES_1) ## Sample luminance in lux lux = str(round(lux, 1)) oled.fill(0) oled.text("LUX",20,0) oled.text(lux, 20,15) oled.show() sleep(0.5)


Why Raspberry Pico and Thonny IDE

Raspberry Pico is a tiny, low-cost microcontroller board designed for DIY projects and programming. Thonny is a free, beginner-friendly Integrated Development Environment (IDE) for Python programming. In this blog, we will discuss how to code the Raspberry Pico using Thonny.

Step 1: Install Thonny Thonny can be downloaded and installed from the Thonny website. It is available for Windows, macOS, and Linux. Once installed, launch Thonny and select the Raspberry Pico as the device to be used.

Step 2: Connect the Raspberry Pico Connect the Raspberry Pico to your computer using a USB cable. Thonny will automatically detect the device and display its status in the status bar.

Step 3: Write Your First Program In Thonny, click on File > New to create a new Python script. Enter the following code to make an internal LED blink:


CODE Raspberry Pi Pico:

from machine import Pin import time led = Pin(25, Pin.OUT) while True: led.value(not led.value()) time.sleep(1)


Raspberry Pi Pico








CODE Raspberry Pi Pico W:

from machine import Pin import time led = Pin("LED", Pin.OUT) while True: led.value(not led.value()) time.sleep(1)

Raspberry Pi Pico W








Step 4: Upload the Code

Save the script and click on Run > Run current script to upload the code to the Raspberry Pico. You should see the LED start to blink.

Step 5: Debugging Thonny provides an easy-to-use debugger that allows you to inspect variables and step through your code. To access the debugger, click on Debug > Debug current script.

Step 6: Interacting with Hardware The Raspberry Pico has a variety of digital and analog inputs and outputs that can be used to interface with sensors, actuators, and other devices. The machine module provides a simple API for working with these hardware components.

In conclusion, Thonny is a great tool for programming the Raspberry Pico and Pico W, making it easy to write, upload, and debug code. Whether you are a beginner or an experienced programmer, Thonny provides a user-friendly environment for exploring the capabilities of the Raspberry Pico and bringing your projects to life.