#369 Definitive Guide to Attaching Sensors to the Raspberry Pi (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how to connect sensors to a raspberry pi where to get the needed libraries does everything just work or do we need some tricks gritsy youtubers here is the guy with a swiss accent with a new episode and fresh ideas around sensors and microcontrollers remember if you subscribe you will always sit in the first row the raspberry pi is very good at managing and displaying data from your sensors via network today we discuss possibilities to attach sensors directly to a raspberry pi we will cover digital sensors one wire sensors i square c sensors sensors analog sensors and serial sensors and we will have a look at how to program the pi to read the sensors all raspberries offer a connector to connect sensors let's look at it on this drawing the 40 pins are colored for ease of use these black pins are ground orange pins are 3.3 volts and the red ones are 5 volts attention raspberries are damaged if we connect 5 volts at any pin so always check your sensors output either with the multimeter or the oscilloscope before connecting them to a pin raspberries are much more expensive compared to an arduino or an esp board for my experiments i usually use this so-called t-cobbler a connection to the breadboard it is easy to use because it has labeled pins but pay attention the 40 pin connector does not fit with all cases here for example the space is too small some of my t-cobblers had wrong cables so the pins were not at all as labeled very dangerous this is why i checked them with a multimeter to be sure i placed the minus probe on the usb connector and the plus probe at 3.3 respectively 5 volts if those pins show the right voltages the chance the rest is ok is very high another possibility is to connect our wires directly to the header and use such a gpio reference cardboard i never liked these boards because the connectors do not stick with a full length this is why i dumped them the 17 green pins are general purpose and can be used to attach sensors and other things the rest are special purpose pins some of them like gpio2 and 3 are later used in this video as with the arduino the gpios are numbered this numbering is called bcm numbering some libraries can also use the board pin numbers by the way with a command pin out you get this information in your terminal i use this laminated printout to have the information always handy next we have to enable the protocols we want to use in the pi configuration i enable spi i square c and one wire for this video serial will be covered later and we have to decide the programming language and libraries we want to use arduinos use the c plus language the most popular language for the raspberry pi is python as we will see it is easy to use and to debug if you do not use the pi desktop you can write python programs using your preferred editor and start them in your terminal window if you are a beginner i strongly suggest using the pi desktop because the useful tony is only available there it is already pre-installed and simple to use has auto completion and even can debug your programs by the way if you enable vnc on your pi you can use the desktop remotely without attaching a display or a keyboard let's get started with the simplest sensors digital sensors which only output on or off for such sensors as well as to switch leds we could use the gpio0 library unfortunately this library is quite limited if it comes to sensors fortunately we have a much better possibility circuit python is an open source project for mcus heavily supported by adafruit they used their vast experience with arduino libraries to donate libraries for circuit python we can also use those libraries on the pi because the circuit python drivers are now ported currently 257 circuit python libraries are listed and the best we will be able to use the same libraries with our esp32 once we plunge into micropython on this platform it is easy to set up these libraries with these four commands i leave a link in the description where you can copy paste the commands the second command is not needed if you use a recent pi image if you want to use my examples you have to add this command it copies the needed files into the directory raspberry sensor tutorials where you can load them with tony now we are ready to rumble let's start with typical digital sensors a button and a pir sensor buttons usually are attached between gpio and ground we have to either add a 10k pull up resistor or to enable an internal pull up resistor the reading of the sensor pins is simple we define a pin as with the arduino and then we read its value in our example an led is switched on when we press the button or the pir sensor triggers both sensors are treated the same way the only difference is the polarity the switch is low or false if pressed and the pir sensor is high or true when triggered next we attach this simple one wire ds18 b20 temperature sensor with this command we can check if one wire is enabled default is gpio pin 4 but because my argon case uses gpio4 we have to use a trick and change it to gpio5 by editing the config.txt file right at the bottom we add this line now we can connect the sensors to gpio5 because this sensor is attached as a device in linux its reading is different you do not need to understand everything just use these two functions and it works we get a vast selection of sensors using the i-square c-bus and many of them are supported by the circuit python libraries so the chance the sensor for your project is supported is high let's test a simple bmp 280 temperature humidity and pressure sensor we first have to install the library with this command this time we have to connect it to the two i square c pins which are gpio 2 and the usual rules apply if you add more sensors or extend the connection cable's length you have to add pull up resistors to the two data lines for one sensor close to the pi these resistors are not needed now we can check the address of the sensor with this command and see that it answers on 76 hex the library works similarly to the arduino we have to open the i square c connection and create a sensor object then we can read the values because my sensor uses 76 instead of the standard 77 we have to include this address in the call then we can set the c level pressure and read the values in the python loop and it works flawlessly next is the 9dof sensor bno055 again we have to install the library and open the example file it works similarly to the pme 280. this sensor just provides more information but unfortunately the values often are wrong as we see here with the temperature why is that i square c sensors usually work with a clock frequency of 400 kilohertz but the bno 050 seems to need some clock stretching with a raspberry we need another trick we have to reduce the clock speed this is done by adding another line in the config.txt file after rebooting the sensor works ok and delivers proper values i did not check all sensors but assume that the other i-square-c sensors work with similar commands you find a link to the documentation of all sensors in the video description what's next we sometimes use spi sensors like the max 31855 high temperature sensor in our projects so we have to see how we can use those sensors we can check if spi is enabled using these commands both should not trigger an error as before we install the library and load the program max31855.pi unfortunately i was not able to get useful results at least the chip seems to work if i disconnect the probe it immediately discovers the fact but i live in switzerland and not in siberia so the temperatures should be a little higher let's continue with the next sensor class sensors with an analog output other than the arduinos and esps the raspberry pi does not offer analog inputs this is why we have to attach an analog to digital converter for those sensors i use the trusted and cheap ads110 4-channel 16-bit adc it is also connected via i-square-c and its address is 48 hex again we have to install the library and load the program also here the usage of the sensor works similarly to the arduino with this sensor we have four analog inputs and we can add a second converter if we need more just change the address to 49 and connect the other pin to 3.3 volts the last sensors are sensors that provide serial data as an example we use the us100 ultrasonic distance sensor to enable serial data we have to start raspy config disable the shell and enable the serial connection now we can connect the sensor to the rx and tx pins pay attention with this sensor the rx pin has to be connected to the pi's rx pin and tx to the tx pin different from many other sensors now we can install the library and load the program if we use the connection on the expansion connector we have to use def pti s0 if we used an ftdi adapter connected to one of the usb connectors the respective connection would be def tty usb0 the rest is as always that's it with this information you can connect all kinds of sensors to your raspberry pi and use them in your python programs conclusions it is easy to connect all sorts of sensors to your raspberry pi's 40 pin connector if you decide to work in python using the circuit python libraries is an excellent choice programs written for the raspberry pi should also work on the esp32 using circuit python as always you find all the relevant links in the description i hope this video was useful or at least interesting for you if true please consider supporting the channel to secure its future existence thank you bye
Info
Channel: Andreas Spiess
Views: 55,944
Rating: undefined out of 5
Keywords: esp8266, esp8266 datasheet, esp8266 project, esp8266 tutorial, FET, FET tutorial, Field Effect Transistor, greatscott, guide, hack, hobby, how to, iot, lora, lorawan, n-channel, nodemcu, npn, p-channel, pnp, project, simple, smart home, ttgo, tutorial, wemos, wifi, Raspberry Pi, Zoom, Microsoft, Teams, Home Assistant, Docker, Aliexpress, Amazon, Banggood
Id: gnE4v-PcYKQ
Channel Id: undefined
Length: 13min 21sec (801 seconds)
Published: Sun Jan 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.