Raspberry Pi Pico und das 35 in 1 Sensors Kit in MicroPython - Teil 1 - AZ-Delivery

The program examples in the eBook for the Sensorkits are written for the Arduino family based on the programming language C/C ++. This blog series is intended to present examples in the Micropython language. Similar sensors or components are grouped by the programming.

At the beginning, the most important information about the Raspberry Pi Pico or Pico W and the programming environment Thonny are summarized again, in order to start with the "Hello World" of "Physical Computing" - the flashing of an LED. Just like the LEDs, some other components of the kit to GPIOS (General Purpose Input/Output), i.e. pins, are controlled as outputs, so that they are also treated in the first part.

In the second part, the GPIOs are used as inputs, first using the example of the button, to understand the importance of pull-up or pull-down resistances. Then the sensors are treated that work like switches/buttons. Since some of the sensors have both digital and analog outputs, the approaches are also discussed with their special features.

The third part then devotes itself to the sensors who need a special electronic interface, one-wire interface, I2C (= inter integrated circuit interface) and SPI (= serial peripheral interface). Here are other sensors from the range of AZ-Delivery treated that are not part of the kit.

Required parts

1

Raspberry Pi Pico or Pico W

1

35 in 1 sensor kit

old

16 in 1 sensor kit

1

Panboard/Breadboard or Half breakboard

Jumper cable, Button/button

Resistance 220 to 330 Ω, 1 kΩ, 4.7 kΩ, 10 kΩ

optional

more Sensors or Displays


First of all, the Pico W. Pinout diagram is identical to the occupancy of the outward pins with the Pico without WiFi/Bluetooth, only the debug pins are elsewhere.

As with other micro controllers, many of the GPIOs have been proven several times with special interfaces. As announced, we first want to let the built -in LED blink. To do this, we use the Thonny program as a programming environment for Micropython. A detailed explanation of the installation of Thonny and the first "walking tests" see blog series "Raspberry Pi Pico and Thonny with Micropython ”(link to Part 1, Part 2, part 3).

Unlike at the ATMEGA328 Uno If you cannot switch an external LED for internal LED, the corresponding PIN is not led out. And there is a difference between the Pico and the Pico W: GPIO25 is used for the PICO, the PICO W uses this line for the Infineon Chip for WiFi/Bluetooth, on which the LED is then connected.

Code exams

The code for the Pico follows. You import the module machine and instantiate the object led_onboard = machine.pin (25, machine.pin.out).

The version for the Pico W is commented on, in which the string term instead of pin 25 "LED" is used.

 import machine
 import utime AS time
 
 
 # Code line for Raspberry Pi Pico (without WLAN)
 #Led_onboard = machine.pin (25, machine.pin.out)
 
 
 # Code line for Raspberry Pi Pico W (with WLAN)
 LED_ONboard = machine.Pin code("LED", machine.Pin code.OUT)
 
 
 # Simple variant with while true loop
 # While True:
 # Led_onboard.toggle ()
 # Time.sleep (1)
 
 
 # Variant with Try and Except to switch off the LED at keyboard interrupt
 try:
     while True:
         LED_ONboard.toggle()
         time.sleep(1)
         
 except Keyboard interrupt:
     LED_ONboard.off()

With this code and the module machine other microcontrollers with micropython may also be programmed. 

But there is a Micropython module especially for the Raspberry Pi Pi Pico, which based on the Python module Gpiocero has arisen and the name picozero carries. For me, these modules are parade examples for object -oriented programming (OOP); You instantiate an object, e.g. LED, and apply the methods (then the functions are called), e.g. led.on (). That is why I use this module when the components are already implemented.

You search and install Picozero under extras/managed packages ... (English tools/manage packages ...)

First, parts of the Picozero and Time modules are imported. The Picozero module knows the built -in LED as pico_led And do not differentiate between the different variants. The following code works on both the Pico and the Pico W. 

 From picozero import pico_led
 From time import sleep
 while True:
     pico_led.on()
     sleep(0.5)
     pico_led.off()
     sleep(0.5)

As I said, you cannot switch external LED in parallel. That is why we take any other GPIO PIN, e.g. GP15 on the physical pin 20, the lowest on the left side of the above Pinout diagram.

When connecting, please do not forget the resistance (220 to 330 Ω) so that the LED does not damage. In the LED modules of the sensor kits, these pre-resistances are already attached to the mini board. With the BI-Color LED module you need two GPIOs, during the traffic light module and the RGBLED you need three GPIOs plus mass connection (= ground, GND,-)

 

Code:

 From picozero import LED
 From time import sleep
 LED = LED(15)
 while True:
     LED.on()
     sleep(0.5)
     LED.off()
     sleep(0.5)

Code for a simple traffic light cycle with the LED Traffic Light Module

 From picozero import LED
 From time import sleep
 Redled = LED(13)
 YellowLed = LED(14)
 green leather = LED(15)
 while True:
     Redled.on()
     sleep(2)
     YellowLed.on()
     sleep(1)
     Redled.off()
     YellowLed.off()
     green leather.on()
     sleep(2)
     green leather.off()
     YellowLed.on()
     sleep(1)
     YellowLed.off()

With the multicolored LEDs in particular, you not only want to switch the individual colors on or off, but also to mix it as desired. For the dimming of the individual LEDs you need the pulse -wide modulation (PWM, English Pulse Width modulation). There are two options for this:

With the LED class you can do the method Brightness Use a function value between 0 and 1. 

Example: 

 LED.Brightness = 0.5     # Half Brightness

In the RGBLED class, a tupel with 8-bit values ​​(numbers between 0 and 255) for the respective color shares is specified as an argument.

Example: 

 From picozero import Rgbled
 RGB = Rgbled(red=13, green=14, blue=15)    # GPIOS
 RGB.color = (255, 127, 0) # Full Red, Half Green = Yellow

Have you discovered an LED in the kit that doesn't flash visibly? That could be an IR LED that Ky-005 IR Transmitter modules. This IR-LED does not send out visible light in the infrared area, as well as most TV remote controls. I recommend the camera of the smartphone as a tool. The IR signal is displayed on the display of the smartphone. The counterpart is the KY-022 IR Receiver modules, which is also used in Smart Car Kit, for example. The applications for such a module are very extensive, which is why I will not go into it here.

Now to the other components in our kits, which are switched like an LED with the help of a GP output: these are relays and active summer (Active Buzzers).

relay

Relays are among the components that require external voltage supply. So there are three connections for +and s (= signal). The signalpin is connected directly to a GP output, the voltage supply of the one relay in the KIT can be connected to the PIN 40 VBus on the PICO on the PICO,-) to one of the many GND pins (3, 8, 13, 18, 23, 28, 33 or 38). Separate voltage supply is better and necessary for several relays, for example with batteries, because electromagnetic consumers (relay, motors) have physical properties that do not get along well with sensitive electronics. It is important that the GND connections of PICO and external battery are connected.

The LED and the methods on () and off () can be used to switch the relay. A special feature can occur: There are also relay modules in which the signalpin must be switched to GND (low) so that the relay attracts.

It is switched in a galvanically separate circuit, i.e. there is no electrical connection to control electronics. Of the three screw connections, the middle is the entrance, the two outer outputs with the names no = Normally Open and NC = Normally Closed. This means that when the relay is put on, the NC output is opened and closed.

Active Summer (Active Buzzers)

First of all, it must be explained what the difference between active and passive sumers (buzzers) is. The active buzzer has its own modulator in his housing, which leaves the little speaker loudly and audibly, but always sums up with the same frequency. You can see it on the two legs of different long legs (the longer leg is +, the shorter -). Often there is still a small sticker with + and - and the text "Remove After Washing" on the top over a small hole. What is meant here is "after the soldering". Recommendation: Do not remove stickers, the beasts are so loud enough when attempting students. This type of buzzer can be found in the household in the washing machine, for trucks when reversing, etc. 

Example code with Picozero:

 # Active Buzzer That Plays A Note Whe Powered
 From time import sleep
 From picozero import buzzer
 buzzer = buzzer(10) # GPIO PIN
 buzzer.on()
 sleep(1)
 buzzer.off()
 sleep(1)
 buzzer.beep()
 sleep(4)
 buzzer.off()

With the passive summer, the modulation is made in the microcontroller. This is more complex, but also enables different pitches, for example to build a secondon fanfare, or to play a small melody. The Micropython module picozero knows the class Speaker. More on this in the documentation of Picozero (https://picozero.readthedocs.io/en/latest/)

In the second part we will get to know the GPIOs as entrances.

Raspberry piSensoren

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. ESP32 jetzt über den Boardverwalter installieren - AZ-Delivery
  2. Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1 - AZ-Delivery
  4. ESP32 - das Multitalent - AZ-Delivery