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:
- Import the necessary modules:
import machine
import ssd1306
import utime
- Initialize the I2C bus and the SSD1306 display:
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
- Enter the main loop:
- Read the temperature from the internal sensor using the ADC:
- Convert the temperature from Celsius to Fahrenheit using the
c_to_f
function:
- Clear the OLED display using the
oled.fill
method:
- 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)
- Update the OLED display using the
oled.show
method:
- Wait for 1 second before reading the temperature again using the
utime.sleep
function: