Your shopping cart is empty!
Read Sensor Value
- Hao Zhen Goh
- 06 Jul 2023
- Tutorial
- Beginner
- 1004
This guide will show you how to read the analog and digital sensor value from Grove Port or GPIO breakout.
Analog Read
The figure below shows the GPIO that can read analog sensor value.
Note: You may look for the pins labelled "ADC" at the back of the board.
1. While your Robo Pico is connected to your computer, open your editor software (for example Thonny). Select the code.py file in your drive.
2. Connect your sensor module to the Robo Pico, to Grove Port 6 using Grove to 4 female pins / grove cable.
OR connect to the GPIO breakout using male-to-female jumper wires togetther with grove cables (if applicable).
Example module used: Maker Soil Moisture Sensor
3. Copy and paste the following code to your editor.
""" DESCRIPTION: This example code will uses: Robo Pico and Raspberry Pi Pico / Pico W to read analog value on the Maker Soil. This code applicable to other analog sensor. CONNECTION: Robo Pico : Maker Line GROVE 6 - Maker Line Grove GP27 - OUT AUTHOR : Cytron Technologies Sdn Bhd WEBSITE : www.cytron.io EMAIL : support@cytron.io REFERENCE: Tutorial link: https://www.cytron.io/tutorial/get-started-robo-pico-circuitpython-read-analog-sensor-value Maker Soil: https://my.cytron.io/p-maker-soil-moisture-sensor """ #Import necessary libraries import board import time import analogio #Define analog pin GP27 used on the board sensor = analogio.AnalogIn(board.GP27) while True: #Serial print the sensor value every 1 second raw_value = sensor.value voltage_value = (raw_value * 3.3) / 65536 print('Raw Value : ', raw_value) print('Voltage Value : ', voltage_value) print('-------------------------') time.sleep(1)
4. Flash the codes to your Raspberry Pi Pico / Pico W. And your CIRCUITPY drive should look like this (no lib file is used):
Digital Read
We will show all 3 types of connections for this application, including Grove Ports, Maker Port and GPIO Breakout.
In this example, we will use the generic module of HC-SR04 Ultrasonic Sensor as illustration.
Grove Port and GPIO Breakout
1. While your Robo Pico is connected to your computer, open your editor software (for example Thonny). Select the code.py file in your drive.
2. Connect your sensor module to the Robo Pico, to Grove Port 4 using Grove to 4 female pins / grove cable.
OR connect to the GPIO breakout using male-to-female jumper wires.
3. Copy and paste the following code to your editor.
""" DESCRIPTION: This example code will uses: Robo Pico and Raspberry Pi Pico / Pico W to read distance from Ultrasonic Sensor HC-SR04P. CONNECTION: Robo Pico Grove 4 : HC-SR04P GP16 - Echo GP17 - Trig AUTHOR : Cytron Technologies Sdn Bhd WEBSITE : www.cytron.io EMAIL : support@cytron.io REFERENCE: Tutorial link: https://www.cytron.io/tutorial/get-started-robo-pico-circuitpython-read-analog-sensor-value """ #Import necessary libraries import time import board import digitalio import adafruit_hcsr04 #Define pin GP16 and GP17 used on the board sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.GP16, echo_pin=board.GP17) while True: Distance = sonar.distance print('Distance : ', Distance, 'cm') time.sleep(1)
4. Flash the codes to your Raspberry Pi Pico / Pico W. And your CIRCUITPY drive should look like this:
Library file(s) required:
adafruit_hcsr04.mpy
If you don't have this file in your computer, download the lib bundle here and search for this file inside the "lib" folder.
Maker Port
1. While your Robo Pico is connected to your computer, open your editor software (for example Thonny). Select the code.py file in your drive.
2. Connect your sensor module to the Robo Pico, to Maker Port using JST SH to 4 female pins / grove cable.
3. Copy and paste the following code to your editor.
""" DESCRIPTION: This example code will uses: Robo Pico and Raspberry Pi Pico / Pico W to read distance from Ultrasonic Sensor HC-SR04P. CONNECTION: Robo Pico Maker Port : HC-SR04P GP2 - Echo GP3 - Trig AUTHOR : Cytron Technologies Sdn Bhd WEBSITE : www.cytron.io EMAIL : support@cytron.io REFERENCE: Tutorial link: https://www.cytron.io/tutorial/get-started-robo-pico-circuitpython-read-analog-sensor-value """ #Import necessary libraries import time import board import digitalio import adafruit_hcsr04 #Define pin GP16 and GP17 used on the board sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.GP3, echo_pin=board.GP2) while True: Distance = sonar.distance print('Distance : ', Distance, 'cm') time.sleep(1)
4. Flash the codes to your Raspberry Pi Pico / Pico W. And your CIRCUITPY drive should look like this:
Library file(s) required:
adafruit_hcsr04.mpy
If you don't have this file in your computer, download the lib bundle here and search for this file inside the "lib" folder.
Hardware Components
Robo Pico
S$21.29 S$21.29