37 Pcs Sensor Kit Tutorial for Raspberry Pi Wiki - DFRobot

1.Introduction

Go interactive with your applications with this new sensor kit! 37 sensors are included in one kit, which definitely gives you a bigger bang for your buck. In the version of this sensor kit, we add more sensor modules like, color sensor, digital wattmeter, heart rate monitor sensor, conductivity sensor switch, and digital shake sensor. Besides that, there are RGB LED module, speaker, vibration motor module, DC motor module and RGB backlight display to help you build up your interactive projects.

Use a variety of sensors to realize your brilliant idea! All of our sensors employ Gravity standard interface, plug and play, also we have a huge amount of code data for your reference. Plug everything in our IO expansion board, burn the codes into it, and then you are ready to go.

With everything included in this kit, you will be able to sense color, heart rate, light, temperature, gas, humidity, flame, direction or let you project speak, shine, and display text. It can almost meet all your needs but the price is more favorable!

This sensor kit supports the Raspberry Pi and Arduino Programming platform as well as visual programming software like mind+, you can get started easily with them!

2.Get Started with Raspberry Pi

Learning Objectives

Learning Contents

Introduction

What is Raspberry Pi?

The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. It is a capable little device that enables people of all ages to explore computing, and to learn how to program in languages like Scratch and Python. It’s capable of doing everything you’d expect a desktop computer to do, from browsing the internet and playing high-definition video, to making spreadsheets, word-processing, and playing games. With the release of Windows 10 IoT, We will also be able to use the Raspberry Pi to run Windows.

Raspberry Pi was developed by the Raspberry Pi Foundation, a UK charity, with Eben·Upton in Cambridge University as the project leader. In March 2012, he officially released the world’s smallest desktop computer, also known as a card computer, which is only the size of a credit card but has all the basic functions of a computer. This is the Raspberry Pi computer board. The foundation aims to improve the education of computer science and related subjects in schools to make computers interesting. And it expects that this computer will continue to be developed and applied to more fields, whether in developing or developed countries. In 2006, the early concept of Raspberry Pi was based on Atmel's ATmega644 microcontroller. The first batch of 10,000 sets of Raspberry Pi boards were made by manufacturers in Taiwan and mainland China.

It is an ARM-based microcomputer motherboard with SD/MicroSD card as the memory hard disk. There are 1/2/4 USB ports and a 10/100 Ethernet port around the card motherboard (A type has no network port), which can be connected to a Keyboard, mouse, and network cable, as well as a TV output interface for video analog signals and an HDMI high-definition video output interface. The above components are all integrated on a motherboard that is only slightly larger than a credit card. It has all the basic functions of a PC. But the Raspberry Pi B model only provides a computer board without memory, power supply, keyboard, case or connection.

Differences between Raspberry Pi models

Selection guide

Raspberry Pi Peripherals

Summary

As everyone has a preliminary understanding of Raspberry Pi, now we are going to learn how to use the 37 PCs Sensor kit on Raspberry Pi 4B.

3.Preparation

Hardware

Install Raspberry Pi System

It is recommended to use a SanDisk Class10 SD card with memory storage of 8G and above. The SD card purchased online may already have image burned inside. However, as system is constantly updating and there are so many systems for Raspberry Pi now (you can't play them all), so burning OS by yourself is a necessary skill. Now we will take the Raspbian system as an example.

Download the Latest Disc Image

It is recommended to download Raspberry Pi OS (32-bit) with desktop and recommended software.

Burn to write raspbian image to SD Card on Windows

It will be installed like this:

3.First Time Booting Up

sudo apt-get install build-essential python-dev python-smbus git

cd /tmp

wget https://project-downloads.drogon.net/wiringpi-latest.deb //Down load wiringpi library

sudo dpkg -i wiringpi-latest.deb //Install wiringpi Library

gpio readall //Read GPIO code number

4.Raspberry Pi Programming Examples

Lesson 1: Digital LED Light Emitting Module

Learning Goals

Let’s start our Raspberry Pi journey from lighting up an LED!

In terms of hardware, you'll learn about Raspberry Pi LED and GPIO output, which is important for later projects. In this process, you'll also come into contact with Python programming and you may find that programming isn't as complicated as you think.

Now let's use Raspberry Pi to control a digital LED light emitting module in this basic project.

Learning Contents - LED Flicker

Guide: In the first project, we will learn about the following contents:

  1. Internal circuit resolution of digital LED light emitting module
  2. Basic uses of Thonny Python IDE
  3. Basic Python code for operating GPIO.

Hardware

Connection

Code

import RPi.GPIO as GPIO   # Import the python module provided by the Raspberry Pi
import time      # Import time package to control flicker

LED=12     # Define the pin number to which the LED is connected

  GPIO.setmode(GPIO.BCM)      # Set GPIO mode, BCM mode is universally available to all Raspberry Pi
  GPIO.setup(LED,GPIO.OUT)     # Set GPIO12 to output mode
  while True:             # Execute the following commands in an infinite loop
    GPIO.output(LED,GPIO.HIGH)    # Set the LED signal pin high (i.e. turn on the LED)    
time.sleep(1)       # Delay one second
    GPIO.output(LED,GPIO.LOW)     # Set the LED signal pin low (i.e. turn off the LED)
    time.sleep(1)       # Delay one second

Lesson 2: Digital Push Button Module

Learning Goals

In the last tutorial, we have learned how to simply control the on and off of the LED module. In this tutorial, we will learn about Raspberry Pi buttons and GPIO inputs based on the previous tutorial, which is very important for future projects. In this process, you will be exposed to new Python programs.

Learning Contents – Control LED by the Button

Guide: In this project, we will learn the basic principles of the button module. And we will also consolidate the basic uses of Thonny Python IDE we learned before, as well as the basic Python code for operating GPIO.

Hardware

Connection

Code

import RPi.GPIO as GPIO    # Import the python module provided by the Raspberry Pi
import time    # Import time package to control flicker

LED=12       # Define the pin number to which the LED is connected
Blue=8       # Define the pin number to which the button is connected

GPIO.setmode(GPIO.BCM)    # Set GPIO mode, BCM mode is universally available to all Raspberry Pi
GPIO.setup(LED,GPIO.OUT)    # Set GPIO12 to output mode
GPIO.setup(Blue,GPIO.IN)      #Set GPIO8 to input mode

while True:        # Execute the following commands in an infinite loop
    if GPIO.input(Blue):  # GPIO.input(Blue) will return to the state of GPIO8 and judge it. If GPIO8 is high (that is, the button is pressed), execute the following statement
        GPIO.output(LED,GPIO.HIGH)      # Set the LED signal pin high (i.e. light up the LED)
    else :          # If GPIO8 is low (that is, the button is released), execute the following statement
        GPIO.output(LED,GPIO.LOW)   # Set the LED signal pin low (i.e. turn off the LED)
time.sleep(0.1)   # Delay one second, here is to control the frequency for querying key status

Lesson 3: LM35 Analog Linear Temperature Sensor

Learning Contents

Introduction to LM35 Analog Linear Temperature Sensor

Based on LM35 semiconductor, this sensor produced by National Semiconductor Corporation can be used to detect ambient temperature. It offers a measurement range from -40 °C to 150 °C and a sensitivity of 10 mV/°C. And its output voltage is proportional to the temperature. Moreover, if used in combination with sensor-specific expansion of Arduino board, this sensor can be really easy to achieve interactive effects related to ambient temperature perception.

Commonly-used sensors for temperature measurement include thermocouples, platinum resistance, thermal resistance and temperature semiconductor chips. Thermocouples are commonly used in high temperature measurement. Platinum resistance temperature modules are used in measurement of 800 degrees Celsius, while the thermal resistance and semiconductor temperature sensor are suitable for measuring the temperature of 100-200 degrees or below. With good linearity and high sensitivity, the semiconductor temperature sensor is easy to use.

Precautions

The port layout of the new analog sensor has the following two improvements. Please refer to the blog How to change the layout of the data cable connector. When using this sensor on the IO expansion board, you may need to adjust the layout of the connector. For your convenience, we will make more improvements, so stay tuned.

Use LM35 Analog Linear Temperature Sensor on Your Raspberry Pi

Lesson 4: I2C Digital Wattmeter

Introduction

I2C Digital Wattmeter is a high-resolution, high-precision, large-scale measurement module that can measure the voltage, current and power of various electronic modules and electrical equipment within 26V 8A, and the maximum relative error is no more than ±0.2% (A simple manual calibration is required before using). It can be used for power consumption measurement or battery life evaluation of solar energy systems, battery coulombmeters, motors, controllers or electronic modules.

This module adopts TI INA219 zero temperature drift current/power monitoring chip and 2W high power low temperature drift 10mΩ alloy sampling resistor. The voltage and current resolution can reach 4mV and 1mA respectively. Under the full-scale measurement condition, the maximum relative error of voltage and current measurement can be superior to ±0.2%. It also provides four I2C addresses that can be configured via the 2P DIP switch. The module accurately measures bi-directional high-side currents (current flowing through the power supply or battery positive), which is especially useful in solar or battery fuel gauge applications where the battery needs to be charged and discharged. This status can be simply determined by positive or negative current readings. In the motor applications, the current can be monitored in real time by monitoring whether the motor current is too large due to overload. In addition, you can use this module to measure the power consumption of various electronic modules or the entire project to evaluate battery life.

Use I2C Digital Wattmeter on Your Raspberry Pi

sudo apt-get install i2c-tools

sudo i2cdump -y 1 0x45

-y means cancelling the user interaction process and directly executing the command 1 is the I2C device number 0×45 is I2C device address

Lesson 5: TCS34725 I2C Color Sensor

Learning Contents

Lead-in

Want to know the secret to chameleons’ ability to change color? Want know how the most popular electronic color picking pen picks color? Don't think too complicated, because their principle is really simple. Nature, is the best teacher of mankind. There are so many interesting ideas inspired by creatures.

Through tens of millions of years of derivation, the chameleon has formed a biological instinct, which can be perfectly hidden in the surroundings by changing the protective color of its skin. This is a process from "color picking" to "color matching".

Today, electronic color picking pens use the same principle. First, obtain the RGB three primary color values by detecting the color of the object. Then blend these values to get the color of the object. It's just like kneading plasticine when you were a kid. After knowing a certain ratio, you can knead the color you want.

Introduction to I2C Color Sensor

TCS34725 is a low-cost and cost-effective RGB full-color sensor. The sensor recognizes the surface color of an object through optical sensing. It supports the three primary colors of red, green, and blue (RGB), supports bright light sensing, and can output the corresponding specific values to help restore the true colors.

For a higher precision, it specifically employs a IR blocking filter at bottom to avoid interference from the surroundings, minimizing the infrared spectrum component of the incident light. In this case, it gets a more accurate color management. Besides, this sensor also includes four ultra-bright LEDs to allow itself to work without external light resources. And the module works via I2C bus, featuring PH2.0-4P and XH2.54 (breadboard) interfaces to meet different using requirements.

Use I2C Color Sensor on Your Raspberry Pi

sudo apt-get install i2c-tools

import smbus
import time
bus = smbus.SMBus(1)    

addr = 0x29

CDATAL = 0x94
CDATAH = 0x95
RDATAL = 0x96
RDATAH = 0x97
GDATAL = 0x98
GDATAH = 0x99
BDATAL = 0x9a
BDATAH = 0x9b

while True:
    ClearL = bus.read_byte_data(addr , CDATAL)
    ClearH = bus.read_byte_data(addr , CDATAH)
    Clear = ClearH * 0x100 + ClearL
    print("Clear = 0X%x" %Clear)

    RedL = bus.read_byte_data(addr , RDATAL)
    RedH = bus.read_byte_data(addr , RDATAH)
    Red = RedH * 0x100 + RedL
    print("Red   = 0X%x" %Red)

    GreenL = bus.read_byte_data(addr , GDATAL)
    GreenH = bus.read_byte_data(addr , GDATAH)
    Green = GreenH * 0x100 + GreenL
    print("Green = 0X%x" %Green)

    BlueL = bus.read_byte_data(addr , BDATAL)
    BlueH = bus.read_byte_data(addr , BDATAH)
    Blue = BlueH * 0x100 + BlueL
    print("Blue  = 0X%x" %Blue)

    print("    ")
    time.sleep(1)

Lesson 6: Analog Rotation Sensor

Learning Contents

Introduction to Analog Rotation Sensor V2

As the rotation angle of the ordinary potentiometer is only 300 degrees at most, the accuracy is quite low after distributing the 5V power supply of Arduino to every 1 degree.

So if you want to make a project with precise control of angle or analog quantity, this precision angle sensor is a good choice. Based on a multi-turn precision potentiometer, this sensor can be rotated about 10 turns and subdivide the voltage into 1024 parts. What’s more, it can be combined with the sensor expansion board through the 3P connection line, accurately sensing small changes in rotation.

*This product has been updated. Please refer to the product wiki at the bottom of the page for the specific line sequence of the V2 version. For V1 version, please connect according to the "1, 2, 3" marked on the module's silk screen.

Use Analog Rotation Sensor on Your Raspberry Pi

sudo apt-get install build-essential python-dev python-smbus git

Lesson 7: 130 DC Motor Fan

Learning Contents

Introduction

This module is really interesting. It can be easily driven by Arduino without an additional motor driver board. You can also use the PWM pulse width to adjust its speed, which is suitable for light applications or small DIY. Simple, but useful.

Use 130 DC Motor Fan on Your Raspberry Pi

Lesson 8: JoyStick

Learning Contents

Introduction to JoyStick

The JoyStick produced by DFRobot is made with original high-quality metal PS2 rocker potentiometer. With (X, Y) 2 axis analog output and (Z) 1 button digital output, it can maintain good contact and mechanical properties no matter how you torture it. The 3 signals are respectively connected to the Arduino sensor expansion board through the 3P line, occupying only 3 ports for its control.

Features of its new version:

Precautions

The layout of the new version of the analog sensor port has the following two improvements. Please refer to the blog How to change the layout of the data cable connector. When using this sensor on the IO expansion board, you may need to adjust the layout of the connector. For your convenience, we will make more improvements, so stay tuned.

Use JoyStick on Your Raspberry Pi

Lesson 9: Analog Light Sensor

Learning Contents

Introduction

Based on PT550 environmentally friendly photodiode, this light sensor can be used to detect the intensity of ambient light. It is usually used to produce interactive works that produce special effects with changes in light intensity. The entire module is connected to the IO expansion board with a 3P analog data cable. As long as the color is corresponding, it will not be inserted wrong, really convenient.

This LCD screen can display the detected light intensity of the sensor, which is reflected by the size of the circle

Precautions

The layout of the new version of the analog sensor port has the following two improvements. Please refer to our blog "How to change the data line connector layout" instructions. When using the sensor on the IO expansion board, you may need to adjust the layout of the connector. For your convenience, we will make more improvements, so stay tuned.

Use Analog Light Sensor on Your Raspberry Pi

Lesson 10: Digital Vibration Sensor

Learning Contents

Introduction

What's the simplest way to check vibration with electric devices? Use a vibration switch to turn on and off the circuit through vibration to generate a signal. Despite a simple structure, you can make full use of this vibration sensor with creative thinking, like step counting, Crash warning light and so on. As long as you have ideas, the usage of simple components will change endlessly. As long as you have ideas, the usage of simple components can be varied an infinite number of times.

This is a very simple pedometer that can count the number of steps you take.

Use Digital Vibration Sensor on Your Raspberry Pi

import RPi.GPIO as GPIO
import time
import atexit

LED=12
Vibration=8

atexit.register(GPIO.cleanup)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
GPIO.setup(Vibration,GPIO.IN)

while True:
    if GPIO.input(Vibration):
        GPIO.output(LED,GPIO.HIGH)
    else :
        GPIO.output(LED,GPIO.LOW)
    time.sleep(0.1)

Lesson 11: Analog Sound Sensor

Learning Contents

Introduction

This is a simple and affordable microphone through which the Arduino can sense the level of the sound and convert it into an analog signal. That is, the volume is reflected by the feedback voltage value. The analog data line directly corresponds to the analog port on the IO expansion board V7. Plug in and burn the code, you can use it.

The sensor reads O when completely muted. When there is music nearby, it will read various readings with the volume.

Precautions

The layout of the new version of the analog sensor port has the following two improvements. Please refer to our blog "How to change the data line connector layout" instructions. When using the sensor on the IO expansion board, you may need to adjust the layout of the connector. For your convenience, we will make more improvements, so stay tuned.

Use Analog Sound Sensor on Your Raspberry Pi

Lesson 12: Heart Rate Monitor Sensor

Learning Contents

Introduction

The DFRobot heart rate sensor, despite a tiny size of just a thumb, can monitor human heart rates. Compatible with Arduino main controllers, this plug-and-play module is really convenient to use that it’s equipped with Gravity 3-Pin interface. It uses PPG (Photo Plethysmo Graphy) to measure heart rate, a low-cost optical technology that monitors the human heart rate by detecting blood oxygen levels in subcutaneous capillaries. Besides, this technology features fast response, stable performance and strong adaptability. As being equipped with two mounting holes, the sensor can be wrapped on your finger, wrist, earlobe or other areas where it has contact with your skin.

The module has two signal output modes: square wave and pulse wave, which can be freely switched through the on-board switch. The pulse wave outputs a continuous heart rate waveform, while the square wave outputs a corresponding square wave according to heart rates.

Precautions

  1. This is a static heart rate sensor. Please do not move or press too tightly during its measurement.
  2. This product is NOT a professional medical device and should not be used to diagnose or treat medical conditions.

Use Heart Rate Sensor on Your Raspberry Pi

import RPi.GPIO as GPIO
import time
import atexit

LED=12
Heart_Rate=8

atexit.register(GPIO.cleanup)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
GPIO.setup(Heart_Rate,GPIO.IN)

while True:
    if GPIO.input(Heart_Rate):
        GPIO.output(LED,GPIO.HIGH)
    else :
        GPIO.output(LED,GPIO.LOW)
    time.sleep(0.1)

Lesson 13 Conductivity Sensor Switch

Preface

Lead-in

In this section we will introduce the conductivity switch sensor. We can use it to test the conductivity of the object, and can also use it as a switching device for special occasions.

Introduction

The conductivity switch is to conduct through two exposed conductors. When the two sides are connected through a certain medium, they are turned on. And the sensor can detect whether there is an object connection between the two poles. When the resistance value of the connected object is less than 10M, it can output a high level. You can use it to make a fruit piano, music wind chime, handshake sensor light and other interesting applications.

Hardware

Learning Contents

Connection

Operating Principle

Change the level signal as a switch by conducting two metal sheets.

Schematic

Software

import RPi.GPIO as GPIO    # Import the python module provided by the Raspberry Pi
import time    # Import time package to control flicker

LED=12       # Define the pin number to which the LED is connected
Electrical_switch = 8                        # Define the pin number to which the switch is connected

GPIO.setmode(GPIO.BCM)                    # Set GPIO mode, BCM mode is common to all Raspberry Pi
GPIO.setup(Electrical_switch,GPIO.IN)      # Set GPIO12 to output mode
GPIO.setup(LED,GPIO.OUT)                    # Set GPIO8 to input mode
GPIO.output(LED,GPIO.HIGH)                  #Define LED original value

while True:                               # Execute the following commands in an infinite loop
    key = GPIO.input(Electrical_switch)
    if (key ):                            # Judge whether the button is pressed
        GPIO.output(LED,GPIO.LOW)         # Button pressed, start the micro vibrator
    else :                     # If GPIO8 is low (that is, the button is released), execute the following statement
        GPIO.output(LED,GPIO.HIGH)        # Not start the micro vibrator
time.sleep(0.01)                   # Delay one second, here is to control the frequency of the query key

http://https//www.bilibili.com/video/BV1z54y1b763/

Lesson 14: Analog Voltage Monitoring Module

Preface

Introduction

Based on the resistance divider principle, this voltage monitoring sensor can be used with the Arduino sensor expansion board to detect the voltage and power level, monitoring the power of interactive media works or the robot power supply.

This module can reduce the input voltage by 5 times. Since the maximum Arduino analog input voltage is 5 V, its input voltage cannot be greater than 5 V × 5 = 25 V.

Hardware

Learning Contents

Connection

Program

Lesson 15: Analog Capacitive Soil Moisture Sensor

Preface

Introduction

This is a simple moisture sensor that can be used to detect soil moisture. When the soil is short of water, its output value will decrease, otherwise it will increase. This kind of sensor is mainly used to measure the relative water content of soil, do soil moisture monitoring, agricultural irrigation and forestry protection.

Hardware

Learning Contents

Connection

Schematic and operating principle

The soil moisture sensor judges the soil moisture content by its water level.

As shown in the figure, when the soil moisture sensor probe is suspended in the air, the base of the triode is open, and the output of the triode is 0. When it is inserted into the soil, the resistance value of the soil is different due to the different moisture content. Then the base of the triode will provide a variable conduction current. The conduction current from the collector to the emitter of the triode is controlled by the base, converted into a voltage after the pull-down resistor of the emitter.

Program

sudo apt-get install build-essential python-dev python-smbus git

git clone https://github.com/DFRobotdl/111.git

Lesson 16: URM09 Ultrasonic Sensor

Preface

Introduction

This is an open dual-probe ultrasonic distance measurement module. Equipped with Gravity standard PH2.0-3P vertical mount interface, it outputs analog voltage, compatible with arduino, Raspberry Pi, and other controllers with logic level of 3.3 V or 5 V.

This module comes with temperature compensation to avoid ambient temperature from affecting its measurements. With its analog voltage value output, it can directly read the temperature value by ADC conversion, simplifying the operation and reducing the difficulty. Besides, this sensor has been tested that its effective measurement range for flat walls is 2 – 500 cm, its resolution is 1cm, and the error is about ±1%. Its dual-probe greatly reduces the detection blind area. And the on-board status indicator light is convenient to check the test progress.

Thanks to its small size, this convenient sensor that allows plug and play has strong environmental applicability, high accuracy, wide measurement range, quite suitable for outdoor environments, especially those with rapid temperature changes. It is also an excellent choice for robots to avoid obstacles automatically, car reversing alarms, doorbells, warning alarms, subway safety line prompts, bank and cash machine one-meter line prompts, and so on.

Preparation

Lead-in

URM09 ultrasonic sensor adopts analog voltage output, so we can get the corresponding test distance through conversion. In this section, we will use Thonny Python IDE basic usage and routine adc to do a simple ultrasonic distance measurement.

Hardware

Learning Contents

Connection

When we move the ultrasonic sensor, we can receive feedback to detect different distance values

Software

sudo apt-get install build-essential python-dev python-smbus git

git clone https://github.com/DFRobotdl/111.git

Lesson 17: Digital Touch Sensor

Preface

Introduction

This is a touch switch module based on capacitive sensing that can sense the direct contact of the human body or metal on its metal surface. In addition to the direct touch, the contact with a certain thickness of plastic, glass and other materials can also be sensed. And its sensitivity depends on the size of the contact surface and the thickness of the covering material.

Preparation

Lead-in

We are already clear about the control of Led. Now we are going to use Thonny Python IDE and the basic Python code operating GPIO to control Led by this digital analog sensors.

Hardware

Learning Contents - Control LED with Digital Touch Sensor

Connection

*If there is a metal object or a finger touches the metal piece, pin 8 inputs a high level and triggers a high level on the pin 12, and the LED is on. When there is no metal object or finger touch, the pin 12 is low and the LED is off. *

Programm

import RPi.GPIO as GPIO    # Import the python module provided by the Raspberry Pi
import time    # Import time package for touch time detection

LED=12          # Define the pin number to which the LED is connected
KEY=8           # Define the pin number to which the sensor is connected

GPIO.setmode(GPIO.BCM)        # Set GPIO mode, BCM mode is common to all Raspberry Pi 
GPIO.setup(LED,GPIO.OUT)    # Set GPIO12 to output mode
GPIO.setup(KEY,GPIO.IN)    # Set GPIO8 to input mode

while True:        # Execute the following commands in an infinite loop
    if GPIO.input(KEY):        # GPIO.input(KEY)will return the state of GPIO and judge, if GPIO8 is high (i.e. The sensor received signal)
         GPIO.output(LED,GPIO.HIGH)      #Set LED signal pin high (Light LED on)
   else :          # If GPIO8 is low (Not receive signal)
     GPIO.output(LED,GPIO.LOW)   #Set LED signal pin low (Turn LED off)
time.sleep(0.1)   # Delay one second, here is to control the frequency of the query key

Lesson 18: Digital Steel Ball Inclination Sensor

Preface

Introduction

This is a digital module based on a steel ball switch. It utilizes the characteristics of the steel ball to roll it towards the bottom through gravity, thereby closing or opening the switch. So it can also be used as a simple tilt sensor.

This module can also be used in combination with the Raspberry Pi sensor expansion board. In this case, it can be used to make very interesting interactive works, which is safer than using a mercury switch.

Operating Principle

It utilizes the characteristics of the steel ball to roll it towards the bottom through gravity, thereby closing or opening the switch.

Requirements

Hardware

Learning Contents

Connection

Program

import RPi.GPIO as GPIO
import time

LED = 12
dip_key = 8

GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
GPIO.setup(dip_key,GPIO.IN)

while True:
    if GPIO.input(dip_key):
        GPIO.output(LED,GPIO.HIGH)
    else:
        GPIO.output(LED,GPIO.LOW)
time.sleep(0.1)

Lesson 19: Digital SMD Magnetic Induction Sensor

Preface

Introduction

This is a magnetic sensor based on high-quality reed tube that can sense the magnetic force within 3cm (the detection distance varies with the magnitude of the magnetic force). With our IO sensor expansion board V7, it can quickly build magnetic interaction projects.

The reed switch is disconnected in an environment without a magnetic field. When the magnetic force is strong enough, the reeds can be contacted and conducted. This process is very fast, making it a highly efficient and reliable switching element.

Requirements

Hardware

Learning Contents

Connection

Program

import RPi.GPIO as GPIO    #Import the python module provided by the Raspberry Pi
import time    #Import time package to detect induction time

LED = 12          #Define the pin number to which the LED is connected
magnetic_key = 8           #Define the pin number to which the sensor is connected

GPIO.setmode(GPIO.BCM)      #Set GPIO mode, BCM mode is common to all Raspberry Pi
GPIO.setup(LED,GPIO.OUT)    #Set GPIO12 to output mode
GPIO.setup(magnetic_key ,GPIO.IN)    #设Set GPIO8 to input mode

while True:        #Execute the following commands in an infinite loop
    if GPIO.input(magnetic_key ):        #GPIO.input(magnetic_key )
      GPIO.output(LED,GPIO.HIGH)         #Set the LED signal high (Turn off LED)
    else :          #If GPIO8 is low (The sensor didn't reiceive signal)
      GPIO.output(LED,GPIO.LOW)          #Set LED signal low (Turn on LED)
time.sleep(0.1)   #Delay one second, here is to control the frequency of the query key

Lesson 20: Mini Vibration Module

Preface

Lead-in

We have already introduced the digital push button module. Now let's use the module and the miniature vibration module to control its vibration to make a simple project.

Introduction

The vibration module uses a vibration motor as the excitation source. The motor is equipped with a set of adjustable eccentric blocks at one end of the rotor shaft, and the excitation force is obtained by the centrifugal force generated by the high-speed rotation of the shaft and the eccentric block.

Requirements

Hardware

Learning Contents

Connection

Schematic

Original Layout

Program

import RPi.GPIO as GPIO                          # Import the python module provided by the Raspberry Pi
import time                                      #Import time package to control flicker



button = 12                                      #Define the pin number to which the button is connected
vibration = 8                                    #Define the pin number to which the vibration is connected

GPIO.setmode(GPIO.BCM)                    #Set GPIO mode, BCM mode is common to all Raspberry Pi
GPIO.setup(button,GPIO.IN)                       #Set GPIO12 to input mode
GPIO.setup(vibration,GPIO.OUT)                   # Set GPIO8 to output mode
while True:                                      # Set GPIO8 to input mode
    key = GPIO.input(button)
    if (key ):                                   #Judge whether the button is pressed
        GPIO.output(vibration,GPIO.HIGH)         #Button pressed, start the micro vibrator
        time.sleep(5)                            #Wait for 5 s
        GPIO.output(vibration,GPIO.LOW)          #Turn off the micro vibrator
    else :                                       #If GPIO8 is low(that is, the button is released), execute the following statement
        GPIO.output(vibration,GPIO.LOW)          #Not start the micro vibrator
time.sleep(0.1)                                  #Delay one second, here is to control the frequency of the query key

FAQ

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents

Product Pictures: KIT0150 product pictures.rar