Showing posts with label SSD1306. Show all posts
Showing posts with label SSD1306. Show all posts

Friday, February 17, 2023

ChatGPT Writes and Explains the Code line by line

ChatGPT Tutorial:

Use chatGPT to help with coding here is one example.

New Chat:

Write code in MicroPython to display temperature reading from internal sensor in celsius and fahrenheit on a SSD1306 display every second. Raspberry Pi Pico board.


Open THONNY IDE copy code from ChatGPT

main.py


import machine

import ssd1306

import utime


# Initialize the I2C bus and the SSD1306 display

i2c = machine.I2C(0, sda=machine.Pin(8), scl=machine.Pin(9), freq=400000)

oled = ssd1306.SSD1306_I2C(128, 32, i2c)


# Define a function to convert Celsius to Fahrenheit

def c_to_f(celsius):

    return (celsius * 1.8) + 32

while True:

    # Read the temperature from the internal sensor

    temp_celsius = machine.ADC(4).read_u16() * 3.3 / 65535 * 100 - 50

    temp_fahrenheit = c_to_f(temp_celsius)

    

    # Clear the display and print the temperature readings

    oled.fill(0)

    oled.text("Temp (C): {:.1f}".format(temp_celsius), 0, 0)

    oled.text("Temp (F): {:.1f}".format(temp_fahrenheit), 0, 10)

    oled.show()

    

    # Wait for 1 second before reading the temperature again

    utime.sleep(1)


ChatGPT explaines the code:


MicroPython code that reads the temperature from the internal sensor and displays it on an SSD1306 OLED display. The code uses the ADC (Analog-to-Digital Converter) to read the temperature and converts it from Celsius to Fahrenheit using the c_to_f function. The temperature readings are then displayed on the OLED display using the oled.text method.

The main loop of the code continuously reads the temperature and updates the display every second using the utime.sleep function.

Here's a step-by-step breakdown of what the code does:

  1. Import the necessary modules:

import machine

import ssd1306

import utime

  1. Initialize the I2C bus and the SSD1306 display:
i2c = machine.I2C(0, sda=machine.Pin(8), scl=machine.Pin(9), freq=400000)

oled = ssd1306.SSD1306_I2C(128, 32, i2c)

  1. Define a function to convert Celsius to Fahrenheit:

def c_to_f(celsius):

    return (celsius * 1.8) + 32

  1. Enter the main loop:
while True:
  1. Read the temperature from the internal sensor using the ADC:
 temp_celsius = machine.ADC(4).read_u16() * 3.3 / 65535 * 100 - 50
  1. Convert the temperature from Celsius to Fahrenheit using the c_to_f function:
 temp_fahrenheit = c_to_f(temp_celsius)
  1. Clear the OLED display using the oled.fill method:
oled.fill(0)
  1. Print the temperature readings on the OLED display using the oled.text method:

oled.text("Temp (C): {:.1f}".format(temp_celsius), 0, 0)

    oled.text("Temp (F): {:.1f}".format(temp_fahrenheit), 0, 10)

  1. Update the OLED display using the oled.show method:
oled.show()
  1. Wait for 1 second before reading the temperature again using the utime.sleep function:
utime.sleep(1)


Pinouts for SSD1306 and Raspberry Pico:











Friday, February 3, 2023

Raspberry Pico and I2C Communication Oled Display SSD1306 and sensor BME280

Raspberry Pi Pico project demo I2C serial communication.

Oled display SSD1306 128x64 and sensor BME280 in same bus.
Thonny micropython code and bus scan address command on the shell.

SCL (Serial Clock Line) and SDA (Serial Data Line) are two of the most important signals used in the Inter-Integrated Circuit (I2C) communication protocol.
I2C is a popular communication protocol used for short-distance communication between microcontrollers, sensors, and other peripherals in embedded systems.

Thonny sample code: from machine import Pin, I2C from time import sleep from ssd1306 import SSD1306_I2C import BME280 i2c = I2C(0,sda=Pin(0), scl=Pin(1), freq=400000) oled = SSD1306_I2C(128,64,i2c) ## Oled Display pixel size 128x64 oled.fill(0) while True: bme = BME280.BME280(i2c=i2c) temp = bme.temperature ## hum = bme.humidity No humidity in BMP280 pres = bme.pressure ## tempf temperature in Fahrenheit tempf = (bme.read_temperature()/100) * (9/5) + 32 tempf = str(round(tempf, 2)) + 'F' ##print('Temperature:', temp ,tempf, ' Pressure: ',pres) ##print('Humidity: ', hum) oled.fill(0) oled.text("TEMPERATURE",20,0) oled.text(temp, 20,15) ### tempf Farenheit oled.text("PRESSURE",20,35) oled.text(pres, 20,50) oled.show() sleep(0.3)

Thursday, February 2, 2023

Oled Display SSD1306

Common module

The SSD1306 is OLED display controller integrated circuit, commonly used in small electronic devices and displays. It´s used to drive small OLED displays 128x64 and provides an I2C interface for communication. It´s commonly used in small projects.


 


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)