System ProgrammingLearn How to Create a Sensor Project using RaspberryPi

Learn How to Create a Sensor Project using RaspberryPi

Sensor Project

Previous articles have focused on clarifying the different communication protocols that can be used in an IoT project. The article also highlighted the different platforms that can be used to build an IoT project. The focus of this article will be demonstrating how to use RaspberryPi to build a sensor. The objective of the sensor project will be to acquire the values that accurately represent the environment state and share them over the Internet.

RaspberryPi is a miniaturized low cost computer that has the capability to run OPEN ELEC, Windows 10 IoT, OSMC, Risc OS and other operating systems customized for specific uses. Besides the listed operating systems RaspberryPi has an official operating system referred to as Raspbian. RaspberryPi has a wide variety of models with varying features to cater for different needs. To support OS management activities such as installation, downloading and set up an OS manager referred to as New Out of the Box Software (NOOBS) is available and it can be downloaded here https://www.raspberrypi.org/downloads/noobs/.

Before any RaspberryPi project can be undertaken, you need a correctly configured set up. You need to select an appropriate model for your task. This article will use model 3 for demonstration. An HDMI supported full or compact display is required, but it is possible to work without a display. To control the Pi, a USB mouse and keyboard are required. The operating system of the Pi is installed on a MicroSD card therefore you need at least an 8 GB card and a card reader. Power supply via micro USB similar to the one used on smart phones is needed.

The first step in setting up a Pi is installing a Raspian or any other preferred OS via NOOBS. The operating system has to be downloaded on another computer other than the Pi and moved to a SD card. The OS can be installed manually or via NOOBS but the NOOBS approach is simpler. Although, manual installation may be more difficult it offers the flexibility to install custom images.

Manual installation begins by downloading a preferred OS from here https://www.raspberrypi.org/downloads/. After downloading an image, you need to unzip it using a tool such as 7-Zip, Unzip or the Unarchiver. To write an image to the SD card an image writing tool such as Etcher available here https://etcher.io/ needs to be used. Download and install etcher, then use it to select an image and an SD card where the image will be written. The SD card is then ready to be inserted on the Pi.

To ensure all devices will be recognized after a boot, there is a specific order of connecting devices. Begin by linking the HDMI cable with the monitor followed by any devices using USB ports. An ethernet cable is connected and finally a power adapter is connected after which the Pi will power up.

To demonstrate code development the C# programming language will be used. To support execution of .NET code the Mono project has to be installed on the Pi using the commands below.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mono-complete

To support development of IoT projects, there are several Clayster libraries available at Github. The data library provides a way of persisting data in the SQLite database that is part of Raspberry Pi. The EventLog library enables logging. The Internet library provides communication protocols that enable communication over the Internet. The Language library enables development of applications that can be internationalized or localized. The Raspberry Pi library provides a way to interact with GPIO pins.

The environment states that will be sensed by our project are motion, temperature and light. The light and temperature sensors are analog and they are connected through an I2C bus to GPIO pins. To communicate the status of the Pi 4 LEDs are used. A green LED on GPIO23 shows application status. A yellow LED on GPIO 24 will show the measurement status. A LED on GPIO 18 will show the status of HTTP activities. A red LED on GPIO 25 will alert us on occurrence of communication errors.

To interact with the Pi hardware Clayster libraries mentioned earlier will be used. A constructor is used to initialize, interact and dispose a hardware resource. It is important to always dispose a resource before application termination because the OS has no control over hardware resources.

To interact with LEDs the code shown below is used: –

private static DigitalOutput executionLed =
new DigitalOutput (23, true);
private static DigitalOutput measurementLed =
new DigitalOutput (24, false);
private static DigitalOutput errorLed =
new DigitalOutput (25, false);
private static DigitalOutput networkLed =
new DigitalOutput (18, false);

To interact with the motion sensor the code shown below is used: –

private static DigitalInput motion = new DigitalInput (22);

To interact with the temperature sensor the code shown below is used: –

private static I2C i2cBus = new I2C (3, 2, 400000);
private static TexasInstrumentsTMP102 tmp102 =
new TexasInstrumentsTMP102 (0, i2cBus);

To interact with the light sensor the code shown below is used: –

private static AD799x adc =
new AD799x (0, true, false, false, false, i2cBus);

The variables that will be used to sense the state of the environment are shown below: –

private static bool motion = false;
private static double tempCelsius;
private static double lightDensity;
private static object synchObject = new object ();

To support retrieval of historical records the variables shown below will be used: –

private static List<Record> everySec = new List<Record> ();
private static List<Record> everyMin = new List<Record> ();
private static List<Record> everyHr = new List<Record> ();
private static List<Record> everyDy = new List<Record> ();
private static List<Record> everyMon = new List<Record> ();

In this article, we introduced Raspberry Pi and noted the OS it can run. We discussed the different hardware devices needed and how to install an OS. We noted the libraries and hardware components needed for a functional sensor. Finally, the code used to interact with hardware components was illustrated.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -