Chapter 12. Sensors

12.0 Introduction

This chapter looks at sensors that convert some physical measurement such as temperature, light, or physical movement into an analog or digital electronic signal.

Many different sensors are explained, and where appropriate, ways of using them with an Arduino or Raspberry Pi are included.

12.1 Connect a Switch to an Arduino or Raspberry Pi

Problem

You want to be able to convert a mechanical movement to a digital on/off signal that you can use with an Arduino or Raspberry Pi.

Solution

Connect the switch between GND and a GPIO pin set to be a digital input as shown in Figure 12-1 and enable the GPIO pin’s internal pull-up resistor.

F12_01
Figure 12-1. Connecting a Switch to a GPIO Pin

Your software will probably also need to “debounce” the signal from the switch.

Get Electronics Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.