Posted on Leave a comment

How to Read internal temperature sensor of Raspberry Pi Pico using Thonny IDE

The internal temperature sensor of RP2040 is read using micropython. The Thonny IDE is used in Windows environment.

The temperature sensor is connected to Channel number 4 of the ADC.

But since this is a rather small implementation using Thonny IDE, it does most of the interfacing related code in the background.

Code

import machine
import time

"""
Function Name: Read internal temperature sensor
Description: This function reads the internal temperature sensor of RP2040 chip.
             The temperature sensor measures the Vbe voltage of a biased bipolar diode,
             connected to the fifth ADC channel (AINSEL=4).
"""
def read_internal_temperature_sensor():
    tsi = machine.ADC(machine.ADC.CORE_TEMP)
    temp = tsi.read_u16() * (3.3 / (65535))
    temp = 27 - (temp - 0.706)/0.001721
    return temp

while True:
    #reads the temprature and prints it
    print("Temperature: ", read_internal_temperature_sensor())
    #Create a dealy of 1 second
    time.sleep(1)
Posted on Leave a comment

How to setup Raspberry Pi Pico using Thonny IDE on windows and Blink onboard LED

First you have to install micropython on your raspberry pi pico board.
Follow the steps shown in the video.

To blink the onboard LED you can follow these steps:

  1. Connect your Raspberry Pi Pico to your Windows computer using a micro USB cable.
  2. Open Thonny IDE on your Windows computer.
  3. Click on the “File” menu and select “New”.
  4. In the new window, enter the following code:
import machine
import time

led_onboard = machine.Pin(25, machine.Pin.OUT)

while True:
    led_onboard.toggle()
    time.sleep(1)
  1. Save the file with a meaningful name and the “.py” extension.
  2. Click on the “Run” menu and select “Run current script” or press the “F5” key.
  3. Thonny IDE will run the code and the onboard LED of Raspberry Pi Pico will start blinking at a frequency of 1 second.

That’s it! You have successfully blinked the onboard LED of Raspberry Pi Pico using Thonny IDE on Windows.

Posted on Leave a comment

How to blink onboard LED on Raspberry Pi Pico W using Thonny IDE in Windows

The raspberry pi pico w has a LED on it.
This LED is not connected to the GPIO pins of RP2040 microcontroller directly.

As you can see in the image of the pinout taken from the official datasheet.
The onboard LED is connected to a pin ‘WL_GPIO0’.
WL_GPIO0 is an internal pin.

There are different ways to program the pico W.
The easiest method is to install thonny IDE. And install micropython on the pico w.

Download Thonny IDE

After you have installed thonny. Now connect you raspberry pi pico w board to the computer USB port while holding the onboard BOOTSEL button. Then follow the steps shown in the following images.

After you have done the above steps. You now have to install a MicroPython library.

picozero is a MicroPython library which has functions for Wifi and other RP2040 chip.

To complete the projects in this path, you need to install the picozero library as a Thonny package.

In Thonny, choose Tools > Manage packages.

Code

import machine
import time

# create a Pin object to control the LED on pin 'WL_GPIO0'
led_pin = machine.Pin('WL_GPIO0', machine.Pin.OUT)

# enter an infinite loop
while True:
    # set the LED pin to a high (on) state
    led_pin.value(1)
    # pause the program for one second
    time.sleep(1)
    # set the LED pin to a low (off) state
    led_pin.value(0)
    # pause the program for one second
    time.sleep(1)

In this program, we first import the machine module and the time module. The machine module provides access to hardware-level features on the Raspberry Pi Pico, while the time module provides functions for time-related operations.

Next, we create a Pin object called led_pin to control the LED connected to the pin labeled ‘WL_GPIO0’. The machine.Pin() function is used to create the led_pin object, with the first argument specifying the pin label and the second argument specifying that the pin is an output pin, i.e. we can set its state to high or low.

Then, we enter an infinite loop using the while True: statement. Within the loop, we use the value() method of the led_pin object to set the state of the LED pin to high (on) or low (off), with a one second delay between each state change using the time.sleep() function.

Comments are added to explain each line of code and make it easier to understand the purpose and function of the program.

Posted on 1 Comment

How to start a project for STM32L476 Discovery in STM32 Cube IDE

Download the STM32 Cube IDE from https://www.st.com/en/development-tools/stm32cubeide.html

Watch this video tutorial.

Write a program using STM32L476G-DISCO

Step1: Install STM32 Cube IDE

Step2: Create a New Workspace
A workspace is a directory which will contain all your code and library related to your project.

Step 3: click on “Start new STM32 Project” button

Step 4: Select your board “stm32l476G-DISCO”

Step 5: Give your project a name
Do not change any options

Step 6: Select “YES” when prompted for “Initialze all peripheral with their default mode”
It is very important that you select YES other wise it will remove all the associated peripherals and you have to manually add the desired peripherals one by one; which is very difficult for beginners.

After these steps your project is created and it will open a STM32 CUBEMX inside your IDE.

Please watch the video. As there are a lots of steps and instruction which are difficult to explain by writing alone. Video will show you a step by step procedure and give you a basic explainition.

You can now change the option or you can proceed forward and click on generate code. This will create all the required code changes.

Now you can open you “main.c” file inside your IDE and write code.