Photo Interrupt Sensor and Hall Magnetic Field Sensor – Project Blog 2


Hello! Welcome to our final week blog on this project. In case this is your first time looking at our project I would highly recommend you go back to our project blog 1 to view an introduction to what we are doing on this blog.

Since our last project blog, we have developed a working prototype using both an encoder motor and the photo interrupt sensor.  Unfortunately we have had to cut back on the complexity, however, we have still kept the core project idea intact.

We have also created a separate blog on Alex Yallop’s account which talks more into an encoder motor if you want to know what they do. Also in that blog, we talk about what booleans are and what a PID library is.


What was our problem?

When integrating our two working codes, we came across an issue involving serial printing and reading. Both of our sensors conflicted as they needed to write to their individual serial monitors.

The photo interrupt sensor module needed a clean input to designate the level of smoke in the air and give instructions appropriately.

The hall magnetic field sensor in our encoder motor needs to have a clean, high refresh rate serial monitor to print the RPM and reliably give it to the motor.

How we have adapted


We have integrated both of our codes, but we have decided to take out the feedback loop to the motor as we have to print to the same serial. Having the feedback loop read from the same serial would cause the code to not function effectively.

What does our prototype do?

Our prototype reads the level of smoke in the air and gives out 4 different values that have different actions attached to them. Depending on the level of smoke, a buzzer activates going up incrementally in the amount of noise. A motor is activated alongside this when the smoke covers the sensor and gives out a value greater than ‘200’. The encoder attached to the motor read the RPM that the motor is giving off.

Our final code is embedded below which you can click on to view in full. In the Code, you can clearly see annotations of what each part of the code does including the four different values and the actions attached to these.


final fritzing diagram motor

Above is our final fritzing diagram for our project. As you can see above, our sensors are not in the default library and we cannot seem to find appropriate components to import. As an alternative, we have used a 6 pin stepper motor to resemble the encoder and a photo interrupter instead of the sensor version.

Above shows the prototype in action. This can be seen on my Youtube channel.

In the video, the photo interrupt sensor is being blocked by a piece of card simulating dense smoke. This triggers the buzzer to sound and starts the motor, the encoder reads the speed of the magnet on the back of the motor. Both the information about the RPM and the level of smoke is displayed on the monitor.




Photo Interrupt Sensor and Hall Magnetic Field Sensor – Project Blog 1

In the upcoming weeks, I will be working with Alex Yallop to produce an extractor fan that detects smoke and activates an encoder motor attached to a fan depending on the level of smoke in the air. We will be using both of our sensors in this project which are a Photo Interrupt Sensor module and a Hall Magnetic Field Sensor.

A Photo interrupter is a transmission-type photosensor, which typically consists of light emitting elements and light receiving elements aligned facing each other in a single package. It works by detecting light blockage when a target object comes between both elements, acting as an optical switch.

A hall magnetic field sensor works by having a thin layer of a semiconductor such as indium antimonide, which when placed in proximity to a magnetic source it polarises the semiconductor. This polarisation courses a small voltage difference, a high gain amplifier is used to increase the signal to be read properly.

If you want to see more about each of these sensors please view mine and Alex’s previous blogs.

Our initial thoughts on how the two sensors would work together is shown in the flow chart below


PIS – Photo Interrupt Sensor

HMF – Hall Magnetic Field Sensor

The Flowchart shows the process of an extractor fan reacting to a smoke level indicator.

The code I have used in my second blog can be used in this instance by adding a feedback loop for the motor module where the different buzzer noises are made, for this project.

We had a problem with using the rpm values from the hall sensor and using it to adjust the speed of the motor. We need to find a way of introducing values to change the rpm of the motor.

We need to design a feedback loop for the motor sensor module, we need to use the rpm value of the Hall Magnetic Sensor and therefore give a stronger or weaker PWM value to adjust the motor. Below shows a feedback loop of the DC motor reacting to RPM.

Feedback occurs when outputs of a system are routed back as inputs as part of a chain of cause-and-effect that forms a circuit or loop

New Doc 2018-02-19_1

In our next week blog, we will be uploading the finished code, flowcharts, and demonstrations of the prototype.



what is a encoder motor

encoder pic


a rotary encoder/encoder motor/shaft encoder are electro-mechanical devices that converts the angular position or motion of a shaft or axle to an analog or digital signal.

encoder motor

within the encoder are a set of hall effect sensors directly pointing at a magnetic timing disc within the armature that is attached to the shaft that is then connected to the gearbox. when the motor is given power the two sensors start reading the number of passes per second (the reason for two sensors is for reliability, accuracy, and redundancy). the encoder then translates and gives the mean value of the two sensors as an output. it is then the job of the controller (the Arduino board) to relate the two and incorporate the gear ratio and therefore can give the rpm of the output shaft.

I have found an experiment of someone using an encoder motor to give values using a void loop to give rpm:encoder motor diagram

//The sample code for driving one way motor encoder
const byte encoder0pinA = 2;//A pin -> the interrupt pin 0
const byte encoder0pinB = 3;//B pin -> the digital pin 3
byte encoder0PinALast;
int duration;//the number of the pulses
boolean Direction;//the rotation direction 
void setup()
  Serial.begin(57600);//Initialize the serial port
  EncoderInit();//Initialize the module
void loop()
  duration = 0;
void EncoderInit()
  Direction = true;//default -> Forward  
  attachInterrupt(0, wheelSpeed, CHANGE);
void wheelSpeed()
  int Lstate = digitalRead(encoder0pinA);
  if((encoder0PinALast == LOW) && Lstate==HIGH)
    int val = digitalRead(encoder0pinB);
    if(val == LOW && Direction)
      Direction = false; //Reverse
    else if(val == HIGH && !Direction)
      Direction = true;  //Forward
  encoder0PinALast = Lstate;
  if(!Direction)  duration++;
  else  duration--;

below I have set up a theoretical experiment of two motors moving an object 1m and are using each others encoder values to make sure the motors both drive at the same rate:



The experiment above, in the video, was to find out how the encoders work with a spinning disc with magnetics attached and how to convert it into readable information. We first of found the correct wiring diagram for the encoder motor and then proceeded to connect and code the two individual motors to count and compare with each other to calculate the RPM and rotation direction of the motor.  The experiment shows how well one of these devices can perform and adding two only improves the reliability and flexibility of the module.

Feedback Loops

A “Closed Loop” system can use the feedback signal to adjust the speed and direction of the motor to achieve the desired result. In the case of an RC servo motor, the feedback is in the form of a potentiometer (pot) connected to the output shaft of the motor. The output of the pot is proportional to the position of the servo shaft.

New Doc 2018-02-19_1


As it turns out, there’s a  mechanism built into all Arduinos that is ideal for monitoring these of real-time events. This mechanism is called an Interrupt. An Interrupt’s job is to make sure that the processor responds quickly to important events. When a certain signal is detected, an Interrupt interrupts whatever the processor is doing, and executes some code designed to react to whatever external stimulus is being fed to the Arduino. Once that code has wrapped up, the processor goes back to whatever it was originally doing as if nothing happened.

What is good about this is that it structures your system to react quickly and efficiently to important events that aren’t easy to anticipate in software. Best of all, it frees up your processor for doing other stuff while it’s waiting for an event to show.

PID Libary

A PID controller calculates an ‘error’ value as the difference between a measured [Input] and the desired setpoint. The controller attempts to minimize the error by adjusting an Output. So, you tell the PID what to measure (the “Input”,) Where you want that measurement to be (the “Setpoint”,) and the variable to adjust that can make that happen (the “Output”.) The PID then adjusts the output trying to make the input equal the setpoint

experiment to make the motor act in conjunction with the rpm.



how could a hall magnetic field sensor be used?

using the hall effect sensor couldn’t be easier, as I’m using a cheaper sensor it is set in a binary configuration, so it is just the case of connecting it to a digital input on the Arduino.

i have set up an experiment to show how it could be used to activate an LED

Arduino Hall Magnetic Sensor Module is a switch that will turn on/off in the presence of a magnetic field.



int led = 13;//LED pin
int sensor = 3; //sensor pin
int val; //numeric variable

void setup()
	pinMode(led, OUTPUT); //set LED pin as output
	pinMode(sensor, INPUT); //set sensor pin as input

void loop()
	val = digitalRead(sensor); //Read the sensor
	if(val == HIGH) //when magnetic field is detected, turn led on
		digitalWrite(Led, HIGH);
		digitalWrite(Led, LOW);


fritzing HMFS

photo HMFS


the next step to this is to attach a set of magnets to a spinning disc and read how many reads per second when put into the serial monitor this can be given a feedback loop to then dictate a set speed. this method is used in encoder motors and some stepper motors.

what is a hall magnetic field sensor?

hall magnetic field sensor or hall effect sensor is a surprisingly common device found in a lot of applications.

the sensor works by having a thin layer of a semiconductor such as indium antimonide, which when placed in proximity to a magnetic source it polarises the semiconductor. this polarisation courses a small voltage difference, a high gain amplifier is used to increase the signal to be read properly.

low-quality high gain amps are most commonly used so a lot of these sensors are viewed ad binary, either on or off. high-quality amps can be used for an analog interface. as of how small these sensors can get and how cheap they are they can be used in quantity to read a digital set of variables.

the most common use cases of these sensors are for motors to calculate its rpm and therefore can be used in tandem with other sensor-motor modules to calculate direction and velocity of the system.




Photo Interrupt Sensor Module – Detecting Smoke

Hello and welcome to my second blog!

In this blog, I will be writing more about this sensor and another one of its many uses, how it can be used as a smoke detector. This blog will link in with my group project which will take place over the next two blogs where we try to make my sensor and a hall magnetic field sensor work together to create an extractor fan which detects smoke levels in a room.

In the diagram below, you can clearly see the maximum input and output voltages that the Photo Interrupt Sensor module can take. In case it is hard to read, at the input (which is the receiver on the sensor) the maximum current it can take is 50 mA (milli-amps). Then at the output (which is the emitter on the sensor), the output current is the same (50mA).

physical characteristics of my sensor

Click to access GP1A57HRJ00F.pdf

design considerations when using my sensor

When considering building a circuit with a Photo Interrupt Sensor module, there are some design considerations you need to take into account. Those of which are outlined in the second table which is above.


On the right, you can see a diagram of how the current flows through the sensor from one side to the other. It travels from the led built into the sensor on the input side of the diagram, the emitter, to the receiver at the output side of the diagram.

I got the image to the right from –

A current use of Photo Interrupt Sensor modules is the use in printers, fax machines and scanners to detect when a printer is out of ink or when a scanner has finished scanning a page so that it can relay that information to the on-screen board for the user to see.

printer sensor

The main problems for an optical sensor, such as a Photo Interrupt Sensor module, are dust and bright light. Dust can usually be blown away. The device lens blocks visible light with a wavelength of 700nm or less.

Using a photo interrupt sensor module for a smoke alarm

fritzing smoke

Above shows how the circuit should be set up using Fritzing to lay it out. Unfortunately, I cannot download the Photo Interrupt Sensor module into Fritzing so the one above is an image to show how it should be connected. The Piezo Speaker is connected to pin 13 and the sensor is connected to the analog pin (A0), the red wires are the positive wires carrying the current and the blue wires are the ground wires.

When the plastic is put between the emitter and the receiver, the piezo speaker is triggered and makes a noise and the phrase ‘slight smoke detected’ is printed on the serial monitor.  Once the plastic is removed it will return to the phrase ‘all clear’  and this will also be printed on the serial monitor.

A smoke detector works in the same way, it detects when light levels drop, when smoke is present, which triggers the sound of the smoke alarm.

Below is a video of me testing the code which can also be viewed on my YouTube channel.



Photo Interrupt Sensor Module – Introduction

Ever wondered how you can turn on a light without even touching it? A photo interrupt sensor could help you. In this blog, I will be explaining the basics of how it works and how you can use it with an Arduino Uno to turn an LED on. Interested?

A Photo interrupter is a transmission-type photosensor, which consists of light emitting elements and light receiving elements aligned facing each other. It works by detecting light blockage when a target object comes between both elements, acting as an optical switch. This also changes the output of the sensor. photo interrupt sensor module pic

There are many components that make up this sensor, it has:

  • An Optical emitter and receiver ,or detector, in the front
  • Two resistors (1K and 33 Ω) in the back.

This sensor also has three pins, these are:

  • One to an analog pin (signal in the diagram above)
  • 5v – provides power to the sensor
  • GND – Ground – take the excess that is not needed and converts it to the ground


Turning an led on using a Photo Interrupt Sensor module


Fritzing diagram found from –

fritzing diagram for first blog - miles weston

Step 1: Set up the Arduino board as shown above

For this circuit to work the following components are needed:

  • Arduino Uno
  • LED
  • 7 jump wires
  • Photo interrupt sensor module
  • A card (to put between the sensor to stop the flow of light)

For this you to work you can either clip the sensor straight into the breadboard or you can acquire some connector wires and attach it that way.

Step 2: The code

I have embedded the code in this blog which you can see below with corresponding code

Step 3: Set up your Arduino board using the fritzing diagram


arduino board for led

If you do not have connecting wires you can set up your Arduino board as shown above.

Step 4: Testing

Put the piece of card between the emitter and the receiver and watch the led turn on when placed in, and off when removed.

video picture 1 - photo interrupt sensor module
Light is currently flowing between the emitter and the receiver
video picture 2 - photo interrupt sensor module
Light is being blocked by the card and the Led is turned on