Figure 1: various sensors

Microcontrollers are very useful in embedded design as they can easily communicate with other devices, such as sensors, switches, displays, keypads, motors and even other microcontrollers. A microcontroller is basically used as the brain or intelligent processing unit to control other devices connected (interfaced) to it in embedded systems just like a PLC is in industrial automation.  

Interfacing a device with a microcontroller in simplest terms simply means to connect a device to a microcontroller. This article will make it easier to anybody with very limited experience in electronics to learn how to interface different sensors widely with a PIC Microcontroller.   

A sensor is a device that is used to detect and respond to events or changes in its environment, and then provide a corresponding output. A sensor is a type of transducer. An electric sensor converts a physical parameter for example temperature, pressure, light, humidity, speed, tilt, moisture, sound, seismic and many more into an electric signal that can be possessed by the microcontroller. For example, a thermocouple generates a known voltage (the output) in response to its temperature (the environment). A Light Dependent Resistor (LDR) changes its resistance based on the amount of light it receives.

The output signal can be an analog voltage changing based on the input physical quantity like the LM35 temperature sensor, or the output can be a digital number like the SHT11 temperature and relative humidity sensor.

The two most important characteristic of a sensor are:

  • Precision: The ideal sensor will always produce the same output for the same input.
  • Resolution: A good sensor will be able to reliably detect small changes in the measured parameter.

In this article we’re gonna learn how to interface various sensors with PIC microcontroller starting with a simple switch to more complex sensors like Relative humidity sensors, smoke detectors and so on.

1. Interfacing a Switch

Figure 2: various forms of switches

Switches are digital inputs and are widely used in electronic projects as most systems need to respond to user commands or sensors. Reading a switch is very useful because a switch is widely used and can also represent a wide range of digital devices in real world like push buttons, limit sensors, level switches, proximity switches, keypads (a combination of switches) etc. 

Switches come in various forms for different purposes, in figure 2 from left to write, we have a push button (makes contact when pushed and breaks contact when released), a toggle switch (Moving the lever back and forth opens and closes an electrical circuit), Slide Switch (when you move the slider from one position to the other, the electric contact is closed or open), Reed switch (a magnetically-actuated switches.When the switch is exposed to a magnetic field, two ferrous materials inside pull together, the connection closes, in absence of a magnetic field, the switch opens) and a limit switch (consists of an actuator mechanically linked to a set of contacts. When an object comes into contact with the actuator, the device make or break an electrical connection).

Connecting a switch to a microcontroller is straight forward, all we need is a pull-up or pull-down resistor.


Figure 3: A switch with a Pull-up resistor        Figure 4: A switch with a Pull-down resistor

The pull-up or pull-down resistor is very important, if there is no resistor it will be difficult to determine the state of the pin, this is called floating.
In figure 3, if the switch is open, the input of the PIC will be high (+5V) and when the switch is closed, the input of the PIC will be low. If there was no resistor, then it could have been a short circuit.
Internal pull-up resistors can also be enabled in software if external resistors are not going to be used, refer to the datasheet to find out more.

To learn more on how to read a switch, go to the Reading Switches with PIC Microcontroller article

Watch the Video Tutorial

 2. Interfacing a Light Dependant Resistor (LDR)

A Light Dependent Resistor (LDR) is a resistor that changes its value according to the intensity of light falling on it. Generally, an LDR has a high resistance in the dark, and a low resistance in the light.


Figure 5: A Light Dependent Resistor   Figure 6: Interfacing a light dependent resistor

In figure 6, the LDR is connected as part of a voltage divider circuit. The output is connected to an analogue input of the microcontroller. The change in resistance of the LDR is thus translated into changes in output voltage that can be read by the analogue input of the PIC. Voltage (Vout) will be: (R1 / (LDR1 + R1)) x 5V

We should also note that the LDR output response is not linear, and so the readings will not change linearly as the light intensity changes, in general there is a larger resistance change at brighter light levels. This should be compensated for in the software by using a smaller range at darker light levels. This same circuit can be used to interface a Thermistor to a PIC microcontroller, the LDR in figure 6 can be replaced by a thermistor.

3. Interfacing LM35 Temperature Sensor

Figure 7: LM35 Temperature Sensor

Temperature sensors are very important in many projects especially in temperature logging devices and alarms. Depending on you application, there are many temperature sensors available some with analog output others with digital output. For very high temperature, thermocouples are usually best suitable.

The LM35 series are precision integrated-circuit temperature sensors from Texas Instruments, whose output voltage is linearly proportional to the Celsius (Centigrade) temperature. Its output is linearly proportional to Centigrade temperature Scale and it changes by 10 mV per °C.

The LM35 does not require any external calibration or trimming to provide typical accuracy of ±1⁄4°C at room temperature and ±3⁄4°C over a full −55 to +150°C temperature range.  
The LM35 Temperature Sensor has Zero offset voltage, which means that the Output is 0V when the temperature is at 0 °C. Thus for the maximum temperature value (150 °C), the maximum output voltage of the sensor would be 150 * 10 mV = 1.5V.

It is very easy to interface the LM35 with a PIC, its output pin can be connected to any analog input of the PIC, the VS to +5 supply and the ground to ground. In figure 8, the LM35 is connected to Analog 0 (pin 2 of PIC18F2620).

Digital Thermometer using PIC Microcontroller and LM35 Temperature Sensor Circuit diagram

Figure 8: LM35 Connected to analog channel 0 of PIC18F2620

Watch the Video Tutorial

4. Interfacing MPX4115A Pressure Sensor

The Motorola MPX4115A is an atmospheric pressure sensor powered by 5V and delivers and output from ~0.25V to ~4.75V based on the pressure detected at room temperature (25°C). The device provides a linear output based on pressure. As the pressure rises, the output voltage of the sensor rises as well with ~0.25V represents <15 kPa pressure relative to a vacuum and ~4.75V represents >115 kPa.
Note that  1 atmosphere of pressure at sea level is equal to 101,325 Pa or 101 kPa.
The graph below shows a typical output response of an MPX4115A pressure sensor, below 15KPa and above 115KPa the voltage doesn’t change.
The MPX4115A is thus an ideal sensor for microcontroller based barometer or altimeter applications.

Extrait from the MPX4114A Datasheet

Figure 9: Graph of the output response of MPX4115A pressure sensor

   MPX4114A Pin Configurations                  MPX4114A Pressure Sensor

Figure 10: MPX4115A Pressure Sensor

5. Interfacing HC-SR04 Ultrasonic Sensor

Figure 11: HC-SR04 ultrasonic Sensor

There are many ultrasonic distance sensors, like the PING and the HC-SR04. The HC-SR04 has become popular because of its low cost Ultrasonic Sensor is a popular and low cost. The HC-SR04 ultrasonic sensor is able to measure distances from 2cm to 400cm with an accuracy of about 3mm. This module includes ultrasonic transmitter, ultrasonic receiver and its control circuit.

The basic principle of operation:

  1. Send a trigger pulse for at least 10us high to the ultrasonic module
  2. The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
  3. If the signal back, ECHO output of the sensor will be in HIGH state (5V) for a duration of time taken for sending and receiving ultrasonic burst. Test distance = (high level time×velocity of sound (340M/S) / 2

HC-SR04 Wiring:

  • Vcc: Connected to 5V Supply
  • Trig pin (Trigger Pulse Input): This pin is connected to any digital Input/Output pin of the microcontroller to be configured as an output pin (TRIS bit is 0)
  • Echo (Echo Pulse Output): This pin is connected to to any digital Input/Output pin of the microcontroller to be configured as an input pin (TRIS bit is 1).
  • GND: Connected to ground (0V)

6. Interfacing DHT temperature & humidity sensors

Figure 12: DHT11 and DHT22 temperature and humidity sensors

The DHT temperature & humidity sensors are very basic and slow, but cheap sensors that can be of great used especially for data logging devices. The DHT sensors are made of two parts, a capacitive humidity sensor and a thermistor. There is also a very basic chip inside that does some analog to digital conversion and spits out a digital signal with the temperature and humidity. The sensor provides fully calibrated digital outputs for the two measurements. The digital signal is fairly easy to read using any microcontroller.

The DHT11 and DHT22 sensors look a bit similar and have the same pinout and work the same way, but have different characteristics. Here are the specs:


  • Ultra low cost
  • Power supply: 3 to 5V
  • Consumption: 2.5mA max current use during conversion (while requesting data)
  • Humidity measurement: 20-80% humidity readings with 5% accuracy
  • Temperature measurement: 0-50°C temperature readings ±2°C accuracy
  • No more than 1 Hz sampling rate (once every second)
  • Body size 15.5mm x 12mm x 5.5mm
  • 4 pins with 0.1″ spacing


  • Low cost
  • Power supply: 3 to 5V
  • Consumption: 2.5mA max current use during conversion (while requesting data)
  • Humidity measurement: 0-100% humidity readings with 2-5% accuracy
  • Temperature measurement: -40 to 125°C temperature readings ±0.5°C accuracy
  • No more than 0.5 Hz sampling rate (once every 2 seconds)
  • Body size 15.1mm x 25mm x 7.7mm
  • 4 pins with 0.1″ spacing

As you can see, the DHT22 is a little more accurate and good over a slightly larger range but also slightly more expensive than the DHT11. Both use a single digital pin and are a bit slow as you can’t query them more than once every second or two

Figure 12: DHT11 and DH22 connection

Only one pin of the sensor, the pin 2 (Data pin) is connected to the microcontroller, pin 1 connected to Vcc of 3 to 5.5V and pin 4 to ground. Pin 3 is not used.

When the connecting cable is shorter than 20 metres, a 5K pull-up resistor is recommended. When the connecting cable is longer than 20 metres, choose a appropriate pull-up resistor as needed.

7. Interfacing a Passive InfraRed (PIR) Sensor

A Passive Infra Red sensor or commonly referred to as PIR are motion-detecting devices used in security systems across the world. A PIR measures infrared light radiating from objects in its area of coverage. Any motion is detected when an object of different temperature (temperature radiates Infrared energy) than the covered area passes through. All objects emit what is known as black body radiation, this Infrared energy is invisible to the human eyes but can be detected by Infrared sensors like the PIR. The PIR will detect the movement of a human, an animal or any moving object entering the monitored area because the infrared energy emitted from the intruder’s body, or whatever was moving is always going to be different than the ambient infrared energy present in the area.

Figure 13: A PIR

A PIR has three pins, the Vcc, the ground and the output pin which can also be called alarm or signal pin. Connecting PIR sensors to a microcontroller is very easy: Connect your Vcc or power pin to positive supply voltage this could be 5V or 12V depending on the PIR you are using, and the ground pin to ground of supply (negative pin). Connect a pull-up resistor to the open-collector signal pin (alarm pin) of the PIR. The PIR acts as a digital input pin to the microocntroller, all you need to do is check for the pin to flip high or low. When the PIR senses motion in it’s viewing area, it pulls the alarm pin low, but when the sensor is inactive, the pin is basically floating that’s why it’s important to use a pull-up resistor to avoid any false-positives, the alarm output should be pulled high to 5V. Most microcontroller’s have internal pull-up resistors on their I/O pins, which can also easily be used to accomplish that task. Figure 14 below shows a prototyping PIR

Figure 14: Prototyping PIR

Figure 15 belows shows a PIR connected to pin RB0 of the microcontroller. Whenever the sensor is inactive, the microcontroller pin RB0 should read high. When motion is detected, the sensor will pull this pin low.

Figure 15: a PIR connected to pin RB0 of PIC18F4520

8. Interfacing a Soil Moisture Sensor

Figure 16: a Soil Moisture Sensor

A Soil Moisture Sensor is used to measure the amount of moisture in the soil or the volumetric water content of soil. This can be very useful to know when your plants need watered or to know how saturated the soil in your garden is. You could include a pump to create an automatic watering System for plants sprinkler.

Measuring soil moisture is important for agricultural applications, it help farmers manage their irrigation systems more efficiently. By Knowing the exact soil moisture conditions on their fields, not only are farmers able to generally use less water to grow a crop, they are also able to increase yields and the quality of the crop by improved management of soil moisture during critical plant growth stages. In urban and suburban areas, landscapes and residential lawns can also use soil moisture sensors to interface with an irrigation controller.

There are many types of moisture sensors, the simplest one is a resistive moisture sensor like the one on figure 16 above. The two probes that you’re gonna insert into the soil act as a variable resistor. The more water is in the soil will results in lower resistance and better conductivity and a higher voltage out on SIG out pin. Your analog readings will vary depending on what voltage you use for Vcc as well as the resolution of your ADC pins.  You will get a SIG out voltage, which will be between almost VCC and GND, depending on the amount of water in the soil.

In terms of connection, the VCC and GND pins of the sensor should be connected to VCC and GND of the supply. It’s also important to note that if you continuoisly power your sensor it will corrode quickly and the more water in your soil, the faster that will happen. To prolong its life you can instead connect the VCC pin to a digital pin and only power it at 5V when you want to take a quick reading, and then turn it off. The SIG out pin can be connected to any analog pin of the microcontroller.