The Pico listens
So far the Pico has spoken. Now it listens. A pin can be an input: the program reads whether there are 3.3 volts on it (1, high) or 0 volts (0, low).
A push button on GP14 connects the pin to GND when you press it – then it reads 0. And when nobody presses? Then the pin is connected to nothing, and something has to decide its level. GPIO::PULL_UP switches on a resistor inside the chip that pulls the pin gently up to 3.3 volts. Not pressed reads 1, pressed reads 0: the button wins against the weak resistor. Run the cell and press the button on the bench (with the mouse, or with the keyboard on it):
require "gpio"
led = GPIO.new(15, GPIO::OUT)
button = GPIO.new(14, GPIO::IN | GPIO::PULL_UP)
# for 10 seconds: press the button on the bench
1000.times do
led.write(button.low? ? 1 : 0) # low = pressed
sleep_ms 10
end
low? is read == 0, high? is read == 1. Now take | GPIO::PULL_UP out of the cell and run it again: the LED stays on, pressed or not. Now nothing pulls the pin up. The RP2350 starts every pin with a weak pull-down, so here it reads 0, "pressed", all the time – and a pin with no pull resistor at all floats and reads whatever it picks up. So a button pin always gets a pull resistor: PULL_UP for a button to GND, as here, or PULL_DOWN for a button to 3V3.
Between 0 and 1: analog values
A button knows two states. A light sensor knows many: the brighter, the more voltage it gives. GP26 to GP28 can measure a voltage – the chip's analog-to-digital converter (ADC) turns it into a number from 0 to 4095 (12 bits). read_raw gives that number, read_voltage the volts, 0 to 3.3. (read gives the volts as well in PicoRuby 4.0.3, although its documentation promises 0 to 1 – the C code decides.) The light sensor here is a photoresistor and a 10 kΩ resistor on GP26; in this simulation its voltage simply follows the light slider:
require "adc"
light = ADC.new(26)
# move the light slider while it runs
20.times do
puts "#{light.read_raw} of 4095 = #{light.read_voltage.round(2)} V"
sleep_ms 200
end
The chip even carries a sensor of its own: a thermometer, on a fifth ADC channel called :temperature. The RP2350's datasheet gives the formula: 0.706 volts at 27 °C, and 1.721 millivolts less for every degree warmer. The chip measures itself, not the room – on a real board it is a few degrees warmer than the air. And the last digit jumps: one step of the ADC is almost half a degree. Move the room temperature slider:
require "adc"
chip = ADC.new(:temperature)
5.times do
volts = chip.read_voltage
puts "#{(27 - (volts - 0.706) / 0.001721).round(1)} °C"
sleep_ms 500
end
I2C: two wires, many chips
Sensor chips that know more than a voltage talk in bytes. I2C needs two wires for that: SDA carries the data, SCL the clock. Many chips share the same two wires; each has an address, a number from 0x08 to 0x77, and only answers when it is called by it.
Waveshare's Pico Environment Sensor is a board with five such chips, on I2C0 with SDA on GP20 and SCL on GP21. scan calls every address and prints who answers:
require "i2c"
i2c = I2C.new(unit: :RP2040_I2C0, frequency: 400_000,
sda_pin: 20, scl_pin: 21)
i2c.scan # asks every address: who is there?
0x76 is the BME280 (temperature, humidity, air pressure), 0x29 a light sensor, 0x53 a UV sensor, 0x59 an air-quality sensor and 0x68 a motion sensor. Two chips on one bus must never share an address: stack Waveshare's DS3231 clock board on top, and it answers at 0x68 too – the two talk over each other.
A driver in a tab
The BME280 sends raw numbers, and turning them into °C, % and hPa takes the datasheet's formulas. They are in a driver: the tab bme280.rb above the code. It is plain PicoRuby you can read – a class that reads and writes the chip's registers through I2C. require "bme280" loads it, just as R2P2 loads /lib/bme280.rb on a real Pico. Move the sliders while it runs:
require "i2c"
require "bme280"
i2c = I2C.new(unit: :RP2040_I2C0, frequency: 400_000,
sda_pin: 20, scl_pin: 21)
bme = BME280.new(i2c)
5.times do
puts "#{bme.temperature} °C, #{bme.humidity} %, #{bme.pressure} hPa"
sleep_ms 1000
end
The button goes between GP14 (pin 19) and GND (pin 18). The light sensor is a photoresistor from 3V3 (pin 36) to GP26 (pin 31) and a 10 kΩ resistor from GP26 to GND (pin 33, AGND): more light, less resistance, more volts on GP26. The Environment Sensor board needs no wires at all – the Pico plugs onto its header. Put the driver on the board as /lib/bme280.rb (the Web Terminal uploads files), then require "bme280" works there too.
require "gpio"
require "adc"
night_light = GPIO.new(15, GPIO::OUT)
sensor = ADC.new(26)
20.times do
# dark (below 1.0 volt)? LED on, otherwise off
sleep_ms 100
end