Showing posts with label ChatGPT. Show all posts
Showing posts with label ChatGPT. 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:











Sunday, February 5, 2023

ChatGPT writes MicroPython code for Raspberry Pi PicoW

See how ChatGPT writes MicroPython code for Raspberry Pi Pico W

ChatGPT is a language model developed by OpenAI, which is capable of generating text based on a given prompt.

Test text for ChatGPT:

Write code in micropython Thonny for Raspberry pi Pico W with sensor BME280 and web server print IP, with homepage for temperature, pressure and humidity.


ChatGPT code:

from machine import I2C, Pin

import bme280 import network import usocket as socket i2c = I2C(0,scl=Pin(1), sda=Pin(0)) # put the right pins bme = bme280.BME280(i2c=i2c) def get_temp(): return bme.temperature def get_pressure(): return bme.pressure def get_humidity(): return bme.humidity def get_html(): html = """<html> <head> <title>Raspberry Pi Pico W with BME280</title> </head> <body> <h1>Temperature: {} C</h1> <h1>Pressure: {} hPa</h1> <h1>Humidity: {} %</h1> </body> </html> """.format(get_temp(), get_pressure(), get_humidity()) return html def main(): sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect("<SSID>", "<Password>")

# Replace <SSID> and <PASSWORD> with your own Wi-Fi credentials.

print("Connected to network with IP: {}".format(sta_if.ifconfig()[0])) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(("", 80)) s.listen(5) while True: conn, addr = s.accept() print("Got a connection from %s" % str(addr)) request = conn.recv(1024) request = str(request) print("Content = %s" % request) response = get_html() conn.send("HTTP/1.1 200 OK\n") conn.send("Content-Type: text/html\n") conn.send("Connection: close\n\n") conn.sendall(response) conn.close() main()



Raspberry Pi Pico W Pinouts

I2C communication

GO0 = SDA GP1 = SCL
GND and 3V3(OUT)



Demo project video: