Human Following Robot Using Arduino and Ultrasonic Sensor

Submitted by Gourav Tak on

In recent years, robotics has witnessed significant advancements, enabling the creation of intelligent machines that can interact with the environment. One exciting application of robotics is the development of human-following robots. These robots can track and follow a person autonomously, making them useful in various scenarios like assistance in crowded areas, navigation support, or even as companions. In this article, we will explore in detail how to build a human-following robot using Arduino and three ultrasonic sensors. Also check all the Arduino based Robotics projects by following the link.

Building a human-following robot using Arduino and three ultrasonic sensors is an interesting project. What makes this project particularly interesting is the use of not just one, but three ultrasonic sensors. This adds a new dimension to the experience, as we typically see human following robot build with one ultrasonic, two IR and one servo motor.  This servo motor has no role in operation and also added unnecessary complications. So I removed this servo and IR sensors and use 3 ultrasonic sensors. With ultrasonic sensors, you can measure distance and use that information to navigate and follow a human target. Here’s a general outline of the steps involved in creating such a robot.

Necessary Components Needed for Human Following Robot

  • Arduino UNO board ×1

  • Ultrasonic sensor ×3

  • L298N motor driver ×1

  • Robot chassis

  • BO motors ×2

  • Wheels ×2

  • Li-ion battery 3.7V ×2

  • Battery holder ×1

  • Breadboard

  • Ultrasonic sensor holder ×3

  • Switch and jumper wires

Human Following Robot Circuit Diagram

Here is the schematic diagram of a Human-following robot circuit.

Arduino Human Following Robot Circuit Diagram

This design incorporates three ultrasonic sensors, allowing distance measurements in three directions front, right, and left. These sensors are connected to the Arduino board through their respective digital pins. Additionally, the circuit includes two DC motors for movement, which are connected to an L298N motor driver module. The motor driver module is in turn connected to the Arduino board using its corresponding digital pins. To power the entire setup, two 3.7V li-ion cells are employed, which are connected to the motor driver module via a switch.

Overall, this circuit diagram showcases the essential components and connections necessary for the Human-following robot to operate effectively.

arduino human following robot circuit

Circuit Connection:

Arduino and HC-SR04 Ultrasonic Sensor Module:

HC-SR04 Ultrasonic sensor Module

  • Connect the VCC pin of each ultrasonic sensor to the 5V pin on the Arduino board.

  • Connect the GND pin of each ultrasonic sensor to the GND pin on the Arduino board.

  • Connect the trigger pin (TRIG) of each ultrasonic sensor to separate digital pins (2,4, and 6) on the Arduino board.

  • Connect the echo pin (ECHO) of each ultrasonic to separate digital pins (3,5, and 7) on the Arduino board.

Arduino and Motor Driver Module:

  • Connect the digital output pins of the Arduino (digital pins 8, 9, 10, and 11) to the appropriate input pins (IN1, IN2, IN3, and IN4) on the motor driver module.

  • Connect the ENA and ENB pins of the motor driver module to the on -board High state pin with the help of female header.

  • Connect the OUT1, OUT2, OUT3, and OUT4 pins of the motor driver module to the appropriate terminals of the motors.

  • Connect the VCC (+5V) and GND pins of the motor driver module to the appropriate power (Vin) and ground (GND) connections on the Arduino.

Power Supply:

  • Connect the positive terminal of the power supply to the +12V input of the motor driver module.

  • Connect the negative terminal of the power supply to the GND pin of the motor driver module.

  • Connect the GND pin of the Arduino to the GND pin of the motor driver module.

Arduino Code for Human Following Robot 

Here is the simple Arduino and 3 Ultrasonic sensor based Human-following robot code that you can use for your project.

Ultrsonic Sensors on Robot

This code reads the distances from three ultrasonic sensors (‘frontDistance’, ‘leftDistance’, and ‘rightDistance’). It then compares these distances to determine the sensor with the smallest distance. If the smallest distance is below the threshold, it moves the car accordingly using the appropriate motor control function (‘moveForward()’, ‘turnLeft()’, ‘turnRight()’). If none of the distance are below the threshold, it stops the motor using ‘stop()’.

In this section, we define the pin connections for the ultrasonic sensors and motor control. The S1Trig, S2Trig, S3Trig, variables represent the trigger pins of the three ultrasonic sensors, while S1Echo, S2Echo, S3Echo, represent their respective echo pins.

The LEFT_MOTOR_PIN1, LEFT_MOTOR_PIN2, RIGHT_MOTOR_PIN1 and RIGHT_MOTOR_PIN2 variables define the pins for controlling the motors.

The MAX_DISTANCE and MIN_DISTANCE_BACK variables set the thresholds for obstacle detection.

// Ultrasonic sensor pins
#define S1Trig 2
#define S2Trig 4
#define S3Trig 6
#define S1Echo 3
#define S2Echo 5
#define S3Echo 7
// Motor control pins
#define LEFT_MOTOR_PIN1 8
#define LEFT_MOTOR_PIN2 9
#define RIGHT_MOTOR_PIN1 10
#define RIGHT_MOTOR_PIN2 11
// Distance thresholds for obstacle detection
#define MAX_DISTANCE 40
#define MIN_DISTANCE_BACK 5

Make sure to adjust the values of ‘MIN_DISTANCE_BACK’ and ‘MAX_DISTANCE’ according to your specific requirements and the characteristics of your robot.

The suitable values for ‘MIN_DISTANCE_BACK’ and ‘MAX_DISTANCE’ depend on the specific requirements and characteristics of your human-following robot. You will need to consider factors such as the speed of your robot, the response time of the sensors, and the desired safety margin

Here are some general guidelines to help you choose suitable values.

MIN_DISTANCE_BACK’ this value represents the distance at which the car should come to a backward when an obstacle or hand is detected directly in front. It should be set to a distance that allows the car to back safely without colliding with the obstacle or hand. A typical value could be around 5-10 cm.

MAX_DISTANCE’ this value represents the maximum distance at which the car considers the path ahead to be clear and can continue moving forward. It should be set to a distance that provides enough room for the car to move without colliding with any obstacles or hands. If your hand and obstacles going out of this range the robot should be stop. A typical value could be around 30-50 cm.

These values are just suggestions, and you may need to adjust them based on the specific characteristics of your robot and the environment in which it operates.

These lines set the motor speed limits. ‘MAX_SPEED’ denotes the upper limit for motor speed, while ‘MIN_SPEED’ is a lower value used for a slight left bias. The speed values are typically within the range of 0 to 255, and can be adjusted to suit our specific requirements.

// Maximum and minimum motor speeds
#define MAX_SPEED 150
#define MIN_SPEED 75

The ‘setup()’ function is called once at the start of the program. In the setup() function, we set the motor control pins (LEFT_MOTOR_PIN1, LEFT_MOTOR_PIN2, RIGHT_MOTOR_PIN1, RIGHT_MOTOR_PIN2) as output pins using ‘pinMode()’ . We also set the trigger pins (S1Trig, S2Trig, S3Trig) of the ultrasonic sensors as output pins and the echo pins (S1Echo, S2Echo, S3Echo) as input pins. Lastly, we initialize the serial communication at a baud rate of 9600 for debugging purposes.

void setup() {
  // Set motor control pins as outputs
  pinMode(LEFT_MOTOR_PIN1, OUTPUT);
  pinMode(LEFT_MOTOR_PIN2, OUTPUT);
  pinMode(RIGHT_MOTOR_PIN1, OUTPUT);
  pinMode(RIGHT_MOTOR_PIN2, OUTPUT);
  //Set the Trig pins as output pins
  pinMode(S1Trig, OUTPUT);
  pinMode(S2Trig, OUTPUT);
  pinMode(S3Trig, OUTPUT);
  //Set the Echo pins as input pins
  pinMode(S1Echo, INPUT);
  pinMode(S2Echo, INPUT);
  pinMode(S3Echo, INPUT);
  // Initialize the serial communication for debugging
  Serial.begin(9600);
}

This block of code consists of three function (‘sensorOne()’, ‘sensorTwo()’, ‘sensorThree()’) responsible for measuring the distance using ultrasonic sensors.

The ‘sensorOne()’ function measure the distance using the first ultrasonic sensor . It's important to note that the conversion of the pulse duration to distance is based on the assumption that the speed of sound is approximately 343 meters per second. Dividing by 29 and halving the result provides an approximate conversion from microseconds to centimeters.

The ‘sensorTwo()’ and ‘sensorThree()’ functions work in a similar manner but for the second and third ultrasonic sensors respectively.

// Function to measure the distance using an ultrasonic sensor
int sensorOne() {
  //pulse output
  digitalWrite(S1Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(S1Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S1Trig, LOW);
  long t = pulseIn(S1Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}
//Get the sensor values
int sensorTwo() {
  //pulse output
  digitalWrite(S2Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(S2Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S2Trig, LOW);
  long t = pulseIn(S2Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}
//Get the sensor values
int sensorThree() {
  //pulse output
  digitalWrite(S3Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(S3Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(S3Trig, LOW);
  long t = pulseIn(S3Echo, HIGH);//Get the pulse
  int cm = t / 29 / 2; //Convert time to the distance
  return cm; // Return the values from the sensor
}

In this section, the ‘loop()’ function begins by calling the ‘sensorOne()’, ‘sensorTwo()’, and ‘sensorThree()’ functions to measure the distances from the ultrasonic sensors. The distances are then stored in the variables ‘frontDistance’, ‘leftDistance’ and ‘rightDistance’.

Next, the code utilizes the ‘Serial’ object to print the distance values to the serial monitor for debugging and monitoring purposes.

void loop() {
  int frontDistance = sensorOne();
  int leftDistance = sensorTwo();
  int rightDistance = sensorThree();
  Serial.print("Front: ");
  Serial.print(frontDistance);
  Serial.print(" cm, Left: ");
  Serial.print(leftDistance);
  Serial.print(" cm, Right: ");
  Serial.print(rightDistance);
  Serial.println(" cm");

In this section of code condition checks if the front distance is less than a threshold value ‘MIN_DISTANCE_BACK’ that indicates a very low distance. If this condition is true, it means that the front distance is very low, and the robot should move backward to avoid a collision. In this case, the ‘moveBackward()’ function is called.

if (frontDistance < MIN_DISTANCE_BACK) {
    moveBackward();
    Serial.println("backward");

If the previous condition is false, this condition is checked. if the front distance is less than the left distance, less than the right distance, and less than the ‘MAX_DISTANCE’ threshold. If this condition is true, it means that the front distance is the smallest among the three distances, and it is also below the maximum distance threshold. In this case, the ‘moveForward()’ function is called to make the car move forward.

else if (frontDistance < leftDistance && frontDistance < rightDistance && frontDistance < MAX_DISTANCE) {
    moveForward();
    Serial.println("forward");

If the previous condition is false, this condition is checked. It verifies if the left distance is less than the right distance and less than the ‘MAX_DISTANCE’ threshold. This condition indicates that the left distance is the smallest among the three distances, and it is also below the minimum distance threshold. Therefore, the ‘turnLeft()’ function is called to make the car turn left.

else if (leftDistance < rightDistance && leftDistance < MAX_DISTANCE) {
    turnLeft();
    Serial.println("left");

If neither of the previous conditions is met, this condition is checked. It ensures that the right distance is less than the ‘MAX_DISTANCE’ threshold. This condition suggests that the right distance is the smallest among the three distances, and it is below the minimum distance threshold. The ‘turnRight()’ function is called to make the car turn right.

else if (rightDistance < MAX_DISTANCE) {
    turnRight();
    Serial.println("right");

If none of the previous conditions are true, it means that none of the distances satisfy the conditions for movement. Therefore, the ‘stop()’ function is called to stop the car.

 else {
    stop();
    Serial.println("stop");

In summary, the code checks the distances from the three ultrasonic sensors and determines the direction in which the car should move based on the 3 ultrasonic sensor with the smallest distance.

Conclusion

Building a human-following robot using Arduino and three ultrasonic sensors is an exciting and rewarding project that combines programming, electronics, and mechanics. With Arduino’s versatility and the availability of affordable components, creating your own human-following robot is within reach.

Human-following robots have a wide range of applications in various filed like they can be used in retail stores, malls, and hotels to provide personalized assistance to customers. Human-following robots can be employed in security and surveillance system to track and monitor individuals in public spaces. They can be used in Entertainment and events, elderly care, guided tours, research and development, education and research, and personal robotics.

They are just a few examples of the applications of human-following robots. As technology advances and robotics continues to evolve, we can expect even more diverse and innovative applications in the future.

Have any question related to this Article?

Introduction to Optocouplers

Have you ever heard the word isolation, especially in electronics? As you might guess, isolation is a key factor when it comes to optocouplers. Isolation is sometimes mandatory and sometimes an extra feature in circuits. Optocouplers are used in many electronic devices, from mobile electronics to household electronics.

So, in this article, let's learn more about optocouplers along with their basics, types, working principles, simulation, hardware demonstration, and live application demonstration. For our demo purposes, we will be using the PC817, a commonly used transistor output optocoupler in electronics.

Starting with a brief explanation of the optocoupler, we begin our walkthrough.

Basics of Optocoupler

In the path of Exploring Optocoupler, let's dig deep into answering questions like WHAT, WHERE, WHY, and HOW.

What is an Optocoupler?

Let's understand the term Optocoupler. It can be separated as OPTO + COUPLER. So, technically, as per the name, it is used as a coupler with the help of some sort of optical technology. In brief, a light source is used as a link between two isolated circuits.

In terms of textual Representation: 

“An optocoupler, also known as an opto-isolator, is an electronic component that transfers electrical signals between two isolated circuits using light. It typically consists of an LED (light-emitting diode) and a photodetector, such as a phototransistor, housed within a single package. When the LED is energized by an input signal, it emits light that is detected by the photodetector, which then produces an output signal. This optical coupling allows the input and output circuits to remain electrically isolated from each other, providing protection against high voltages and electrical noise.”

Here, I would like to add a point that not only optical technology but also electromagnetic induction is used for isolation more commonly.

Where are the optocouplers used?

Commonly the optocouplers are used in the circuits where the isolation is required between any two regions.

Relay Module

For example, let’s consider that we are working on a project where an Arduino UNO-like microcontroller needs to control an AC tube light. In this case, the first thing that will come to mind is a Relay module. Of course, we use a relay module, but do you know exactly why we use a relay module when even a TRIAC can be used to do the same work? Yes, it’s isolation. In the case of a TRIAC, there is a chance of higher AC voltage entering the low-power DC network, which, of course, fries the ICs like chips. So, the relay makes a suitable choice. Yet our concern is not about the relay, it’s the optocoupler. 

A relay is the first level of protection, an optocoupler is also used between the microcontroller and the relay coil as a second level of protection. Being an electromechanical component, a relay could wear out over time. In that rare case, the AC power might touch the coil of the electromagnet inside the relay, which once again creates a path for AC to enter the DC network. This is where the optocoupler comes in handy and isolates both networks. 

Actually, apart from the relay, some types of Optocouplers can be used to switch a TRIAC directly.

Hope you understand the usage of optocouplers. Next, let's know why optocouplers are still preferred to do the job.

Why are Optocouplers Preferred Over Other Options?

The answer is simple. Unlike other options, there is no chance of electrical bonding between the separated regions even in the event of system failure. The possibility is very rare, such as if the potential is greater than the isolation voltage between the input and output of the optocoupler, which is about 5000 volts for the Optocoupler like PC817. That's why I said it's rare. There is no chance of placing such low-power electronics in such a high-voltage area. So, we trust optocouplers more than others.

Now you should have a clear understanding of optocouplers. Let's move the interesting part of how it works.

How Optocouplers Works?

There are numerous ways to understand the Working of the optocoupler. I like to make you to compare the wireless Remote with the optocoupler. Let's look at it in detail.

Optocoupler’s Working Explanation

In the above illustration, you can see the remote car [Output] setup along with the wireless remote [Input]. Each has a separate power source, so the remote needs to be charged separately, and similarly, the car needs to be charged as well. If neither is charged, there is no chance of driving the car. Even if there is some issue with the car, it won't affect the remote, and vice versa. This is because there is wireless transmission and reception technology in between. The overall working will only be affected if some other RF signal interferes with the existing system. So, that's the point I wanted to deliver.

introduction to optocouplers working demonstration

In the Above Animated GIFs, you can see the working of the optocoupler. Like the remote control car, the optocoupler has an LED as an input and a phototransistor as an output. The LED transmits infrared rays, and the phototransistor receives the transmitted infrared waves at its base as a signal, which turns on the transistor. Similar to the remote control car, the functioning of the optocoupler can be disturbed by any external light sources. That’s why the optocoupler is completely sealed to avoid external light interference. Remember, This explanation Using the remote car is only for understanding the concept of Optocoupler.

You might wonder if there is a physical connection between the input and output internally, which may cause any trouble. Ha ha, don't worry; there is a term known as dielectric strength. Usually, the material used to isolate the LED and phototransistor is non-conductive epoxy resin, which has a dielectric strength of Vmax = 20kV/mm. So, let's assume there is a 0.25 mm gap in between, which might require nearly 5000 volts to start conducting. 

Hereby, the working of the optocoupler PC817 is completed.

Types of Optocouplers

Optocouplers can generally be classified into three categories: Based on their Input, Output, and Functions. Let's see each category in detail.

Types of Optocoupler

Types of Optocouplers Based on Input:

Optocouplers can be categorized based on their input types into two divisions: unidirectional input and bidirectional input, also known as DC input and AC input, respectively. The primary difference lies in the configuration of the LEDs within the optocoupler.

  • Unidirectional (DC) Input: This type has a single LED that responds to current flowing in one direction only.

  • Bidirectional (AC) Input: This type features two LEDs connected in opposite directions (one inverted), allowing it to respond to current flowing in either direction, making it suitable for AC input signals.

Types of Optocoupler based on their Input

Types of Optocouplers Based on Output:

Here the optocoupler can be classified based on the type of Output Device used. Some of the used output devices are Photodiode, Phototransistor, Photodarlington, MOSFET, SCR, and TRIAC.

Optocoupler with Photodiode Output:

In this type, the output is a direct photodiode. this optocoupler is widely used in proximity detection, Rotary encoders, and Photo Interrupter sensors.

Optocoupler with Photodiode Output

The above is the image and symbol of the photo-interrupter sensor used for measuring the speed of rotating motors and in many other applications.

Optocoupler with Phototransistor Output:

Phototransistor output optocouplers are widely used due to their simplicity and low cost. In this type of optocoupler, a phototransistor is integrated at the output, providing an easy way to draw output from the device using a load resistor.

Optocoupler with Phototransistor Output

https://components101.com/sites/default/files/component_datasheet/PC817%20Datasheet.pdf

The above is the image and symbol of the PC817, a commonly used optocoupler that has a phototransistor as its output device.

Optocoupler with Photodarlington Output:

Photodarlington output optocouplers are utilized when a higher current transfer ratio (CTR) is required. This type of optocoupler incorporates a Photodarlington transistor pair at the output.

Optocoupler with Photodarlington Output

https://www.vishay.com/docs/83617/il221at.pdf

Above You can see the image and Symbol of IL221AT, an Optocoupler with Photodarlington Output, Low Input Current, High Gain, and Base Connection.

Optocoupler with MOSFET Output:

MOSFET output optocouplers are used in applications that require high-speed and efficient power switching. These optocouplers incorporate a MOSFET at the output, providing several advantages over other types of optocouplers like High-speed Switching, Efficiency, and immunity to Noise

Optocoupler with MOSFET Output

https://www.farnell.com/datasheets/461023.pdf

In the above image, you can see the TLP222A, which consists of an infrared emitting diode optically coupled to a photo-MOSFET in a DIP package. It is suitable for use as on/Off control for high current.

Optocoupler with Triac & SCR Output:

Triac & SCR Output optocouplers are known for its requirement in higher power switching and capability of triggering thyristor and triac on its own. This comes in handy when we need to switch the AC appliance with Triac directly from a microcontroller.

Optocoupler with Triac & SCR Output

https://www.farnell.com/datasheets/3929882.pdf

The above is the image and symbol of the MOC301XM/MOC302XM, which contains a GaAs infrared emitting diode and a light-activated silicon bilateral switch, functioning like a triac. They are designed for interfacing between electronic controls and power TRIACs to control resistive and inductive loads.

Types of Optocouplers Based on Function:

Optocouplers based on Function are designed to perform specific tasks, often integrating multiple Blocks into a single device. There are eight primary types of function-based optocouplers, each tailored for distinct applications. These optocouplers have more complex internals compared to other types due to their specialized nature.  

The most common types are

  1. Logic Output Optocouplers (Eg: 4N35)

  2. High Linearity Optocouplers (Eg: IL300)

  3. High-Speed Optocouplers (Eg: 6N137)

  4. Galvanically Isolated Gate Drivers (Eg: ADuM3223)

  5. Optically Isolated Gate Drivers (Eg: HCPL3120)

  6. Optically Isolated Amplifiers (Eg: HCPL-7800A)

  7. Solid State Relays (SSR) (Eg: G3MB-202P-5VDC)

  8. Voltage and Current Sensors (Eg: ACPL_798J)

To know more about these, you can Explore its example links nearby.

And this might not be the end of the types of optocouplers. There are still many optocouplers out there, of which the above were our basic considerations. So out of these, let's consider the PC817 as an example optocoupler for our following simulations and practical demonstrations. 

Next, let's get introduced to the PC817.

Pinout of PC817 IC

Pinout of PC817 IC

The above image shows the pinout of the PC817, providing a clear explanation of each pin. Below is the pin description of the PC817, explained in the following table:

Pin NoPin NameDescription
1AnodeAnode Pin of Infrared Light Emitting Diode.
2CathodeCathode Pin of Infrared Light Emitting Diode.
3EmitterEmitter Pin of the Internal Photo Transistor.
4CollectorCollector Pin of the Internal Photo Transistor.

Let’s look at some of the important specifications of PC817.

Specifications of PC817

Here's the quick specification table for the PC817:

Specification of PC817

First, let’s look at the input parameters, starting from the anode and cathode side. Consider it as a simple LED. Like a light-emitting diode, it has a forward voltage (Vf) and forward current (If), as shown above. Using these, we can calculate the appropriate resistor to be used in series with the input side. Make sure you are mindful of polarity because the IR LED diode inside has a very low reverse voltage of around 6V, which can permanently damage the LED.

The output part, consisting of the emitter and collector, can be considered as a transistor. As a transistor, it has a maximum collector current of 50mA and a higher collector-emitter voltage range of 80V maximum. Another important factor to consider is the frequency, with a typical cutoff frequency of 80kHz. So, it too has its limitations.

Finally, the operating temperature ranges from -30 to +125 ˚C, and storage should be between -55 to +100 ˚C. While soldering, you can reach a maximum of 260˚C for up to 10 seconds on the pins of the PC817. If the conditions exceed these limitations, the PC817 will be damaged internally.

Next, we are moving to the Stimulation of PC817 Optocoupler.

Stimulation of PC817 Optocoupler in Proteus:

In this simulation section, we will delve deeper into the workings of the PC817, starting with a basic simple simulation of the PC817. 

introduction to optocouplers direct output working demonstration

In the above diagram, you can see the direct output method. Here, R1 is the current-limiting resistor for the IR LED inside the PC817, and a button is connected between R1 and the positive power supply. R2 is the load resistor, which allows you to control the voltage gain and frequency response directly by adjusting this resistor. The output is connected directly to the LED via R3, completing the circuit. When the push button is pressed, the output LED turns off.

Input StateOutput State
HIGHLOW
LOWHIGH

In the above table, you can see the logic state difference between the input and output for the direct method. Now, let’s move to the next method, the inverted output method.

introduction to optocouplers inverted output working demonstration

In the inverted method, everything is the same except for Q1, which is a PNP transistor used to invert the output from the optocoupler, ensuring that the output state matches the input state. Below, you can see the output of the inverted method.

Input StateOutput State
HIGHHIGH
LOWLOW

As the signal is inverted by the PNP transistor Q1, the logic states of the input and output are directly proportional.

Next, we have a bonus simulation of the actual relay module available in the market.

introduction to optocouplers relay simulation working demonstration

Here, the inverted output from Q2 is connected to one side of the relay coil, and the other side is grounded. A diode is connected in parallel to the relay coil to protect the circuit from reverse EMF, and an LED is also connected in parallel to the output for indication.

At the output, the switch of the AC light bulb is connected to the Normally Open (NO) and Common (COM) terminals of the relay. So, when the push button is pressed, the relay turns on, along with the AC light, as shown in the above GIF.

Now let us Move towards the Hardware demonstration of the Optocoupler PC817.

Hardware Demonstration of PC817 Optocoupler:

Below You can see the hardware demonstration of PC817 Optocoupler.

introduction to optocouplers hardware demonstration working demonstration

In this hardware demonstration, the direct output method is applied. Choosing different power supplies helps you understand more about the working. Here, there are two different power supplies, one for the input side and another for the output side. You can see that both sides are perfectly isolated on the breadboard.

You might wonder about taking output directly from the optocoupler by driving the output in a source or sink drive method, which doesn’t invert the signal. Yes, it doesn't invert the signal, but this method is not recommended in the datasheet, even if it requires less current than the maximum collector-emitter current of 50mA. However, if you are confident about your circuit, you can proceed that way.

When you press the button, the LED goes off. This demonstrates the concept of direct output.

Let’s learn more about testing the PC817 Optocoupler.

How to Test Optocoupler?

Testing an optocoupler is very simple and easier than you might think. There are many ways to do that, which we will discuss next.

Test Circuit for Optocoupler:

This method is preferred for professionals who need to ensure that the component meets its specific requirements and operates correctly within the intended application. However, if you are a hobbyist, you can skip this section and move to our next method, where you only need a multimeter to carry out the process.

You can find the test circuit in the datasheet of the respective optocoupler you selected. In our case, it's the PC817. If you explore its datasheet, you will find two test circuits: one to check response time and another to check frequency response. These two test methods require a function generator and an oscilloscope.

Test Circuit For Optocoupler

The above is the test circuit for checking the response time of the optocoupler PC817. Here, a square wave of the desired frequency is passed as an input to the anode side of the optocoupler through a current-limiting resistor Rd. The input square wave is verified using the output received between the load resistor Rl and the collector of the optocoupler. This input and output wave is compared simultaneously using a two-channel oscilloscope, and the deflection in response time can be easily found and classified.

Test Circuit for Frequency Response

The above is the test circuit for checking the frequency response of the optocoupler. As you can see, the hardware setup is the same as above. The only difference is that the input signal’s frequency is adjusted, and you can use the above graph to verify the results. You can adjust the load resistance to set the gain to the required amount. That's how we can check the working using the test circuit provided in the datasheet.

Next, let's look at the easiest and most affordable method.

Using Multimeter For testing Optocoupler:

In this method, the concept is simple: you will consider the input side (anode and cathode) as a diode and the output side (collector and emitter) as a transistor. So, the next step is straightforward. Yes, we keep the multimeter in diode mode and check the optocoupler's input in both forward and reverse bias as follows.

Checking Procedure for Input Side of the Optocoupler

In the illustration above, you will get the following results. In forward bias, you should see a voltage of around 1V with an accepted tolerance of ±0.1V. In reverse bias, you should get no voltage, so "OL" should be displayed on the multimeter, indicating that no current is flowing. This verifies the input infrared LED. If there are any abnormalities, there might be an issue with the LED side.

Next, we need to determine the resistance value to connect to the anode of the optocoupler. You can use a free LED resistance calculator tool to find out the required resistance value. Check the specifications of the optocoupler you are using or use the data below for the PC817 to fill in the input spaces in the tool. Once you have the value, if you don't have that exact resistor, use a combination of series and parallel resistors to approximate it. A slightly higher value is acceptable.
[Screenshot of the Parameters used in our Online Led Resistor Calculator]

 Online Led Resistor Calculator

In my case, it calculated a 190-ohm resistor, but I am using a 220-ohm resistor, which is close enough. Now, follow these steps:

Checking Procedure for Output Side of the Optocoupler

Forward Bias of the Collector-Emitter of the Optocoupler with Connected Input Power:

  • Power up the input side of the optocoupler by connecting the calculated resistance in series with the anode and providing 5V. Connect the cathode to the ground.

  • Set the multimeter to resistance mode. Connect the positive lead to the collector and the negative lead to the emitter. The measured resistance value should be below 100 ohms. In my case, it read 90 ohms. The read resistance is proportional to the power supplied to the infrared LED. For correct calculations, the value should be less than 100 ohms. If it exceeds 100 ohms and moves into the kilo-ohm range, there may be an issue.

Without Powering the Input Side:

  • The resistance should read "OL." If it shows values in the ohm or kilo-ohm range, there may be a short in the transistor part.

This completes the testing process, and you should now understand how to test an optocoupler using a multimeter.

Next, we see a few real-world applications of Optocoupler.

Application Of Optocoupler:

Let's see some of the applications where optocouplers play a crucial role in our DIY projects for a better understanding of the concept.

  1. Relay Modules - Here, the optocoupler PC817 is widely used for isolating the relay side from the main control circuitry.

  2. AC Light Dimmer using Arduino and TRIAC - This project uses two types of optocouplers: a transistor output optocoupler and a TRIAC output optocoupler. The transistor output optocoupler is used to detect the zero crossing of the AC signal, while the TRIAC output optocoupler is used to drive the TRIAC directly, enabling phase angle control using a microcontroller or other circuitry. This is crucial for applications like dimming AC lights and regulating power to AC equipment.

  3. AC Lights Flashing and Blink Control Circuit Using 555 Timer and TRIAC - Similar to the AC light dimmer project, this application also uses both transistor and TRIAC output optocouplers. The transistor output optocoupler finds the zero crossing of the AC signal, and the TRIAC output optocoupler drives the TRIAC for precise control, enabling the flashing and blinking of AC lights.

  4. Raspberry Pi Emergency Light with Darkness and AC Power Line Off Detector - In this project, a transistor output optocoupler is used to drive the MOSFET, which controls the brightness of multiple LEDs. This setup ensures that the emergency light activates in the absence of AC power or in low-light conditions, providing reliable illumination.

  5. Design and Build a Compact 3.3V/1.5A SMPS Circuit for Space Constraint Applications - In this application, the PC817 optocoupler provides feedback of the output to the internal SMPS IC in an isolated manner. This isolation is crucial for maintaining the stability and safety of the power supply, especially in space-constrained applications where efficient and compact design is essential.

Conclusion

I hope you understand this article about optocouplers in detail. Visit our site for more projects that use optocouplers and to gain a deeper understanding of their applications.

Have any question related to this Article?