Photon Control of a MOSFET

This tutorial builds upon both the code base and circuitry as developed with the Particle.publish() and Particle.subscribe() example. This time around instead of registering a subscription that can be used to turn on or off an led we set one up to control a MOSFET.

Figure 1: The completed system

The result is a circuit which can be used to control high power devices such as motors and solenoids via an independent power source.

What is a MOSFET

A MOSFET or metal–oxide–semiconductor field-effect transistor; is as the name suggests a special type of transistor that requires very little current to activate. This feature means that they are perfect for use with micro-controllers. A transistor itself is a 3 lead component that generally has 2 basic functions, to switch or to amplify. In this example we will be using one as a switch.

How a MOSFET Works

As stated a MOSFET has three leads. The Source lead is basically an input whilst the Drain lead is effectively an output. The Gate is a control pin that when activated switches the transistor and allows current to flow from the Source (in) to the Drain (out).

An Overview of the Circuit

The MOSFET is connected so that by default the high powered device is connected to V+ but not to ground (V-). The ground however is connected to the transistor’s drain. When the micro-controller sends a HIGH signal to the transistor’s gate; the transistor switches, (connecting the drain and source) and thus completes the circuit for the high powered device.

Figure 2: The MOSFET circuit

In addition to the aforementioned connections to the MOSFET the circuit also has a resistor (10K) that acts to hold the Gate low when the micro-controller does not send a HIGH signal. This is to prevent false readings caused by floating values on the pin.

The Code

The code for this example is almost exactly the same as that developed for the Particle.publish() and Particle.subscribe() example. The only real difference is the name of the event which has been changed to “mosToggle” rather than that of “ledToggle”.

Additionally all the property names such as those used for the pin references have also been updated. The actual functionality of the code however, remains the same.

The Event Subscription Code

The following code block outlines the setup of the program and the registration of the “mosToggle” event subscription.

void setup()
{
// Set the mosfet pin (set as D3) as an
// output.
pinMode(mosfet, OUTPUT);
// Set mosfet to off by writing
// LOW to the pin.
digitalWrite(mosfet, LOW);
// Register the subscription and assign
// a handler for it
Particle.subscribe("mosToggle", myHandler);
}
view raw MosfetSetup.ino hosted with ❤ by GitHub

Code 1: The event subscription code

The next block of code shows the subscription handlers construction. The handler is instantiated as part of the registration of the “mosToggle” event subscription.

// The handler for the subscription
void myHandler(String event, String data)
{
if(data == "on")
{
digitalWrite(mosfet, HIGH);
}
if(data == "off")
{
digitalWrite(mosfet, LOW);
}
}
view raw MosfetHandler.ino hosted with ❤ by GitHub

Code 2: The event subscription handler

All this code does is set the mosfet pin (D3) to either HIGH or LOW depending upon the data received via the subscription.

The Event Publish Code

The next code block shows the main loop for the button input program. On each iteration of the loop; the value of the button input pin is read and the flag pressed is updated accordingly.

// Loop through the program
void loop()
{
// Check to see if the button is pressed
// or not. The use of flags ensures that
// only a singular event is called for
// each state.
if(digitalRead(buttonPin) == LOW)
{
// The button is released. Check against the flag
// to see if it has already been set. If so return.
if(pressed == false)
return;
// The flag has not been set so set it now.
pressed = false;
// Publish the off event.
Particle.publish("mosToggle", "off");
}
else if(digitalRead(buttonPin) == HIGH)
{
// The button is pressed. Check against the flag
// to see if it has already been set. If so return.
if(pressed == true)
return;
// The flag has not been set so set it now.
pressed = true;
// Publish the on event.
Particle.publish("mosToggle", "on");
}
// Use a delay to slow down the loop; thus allowing
// time for operations to complete.
delay(period);
}
view raw MosfetLoop.ino hosted with ❤ by GitHub

Code 3: The event publish code

The flag pressed is used so that only a single activation of either press or release is used to trigger the event call. What this means is that if a reading of LOW or HIGH is made and the value of pressed has already been set to reflect this state; nothing happens as the code just returns.

The full source code for this example can be found here.

A copy of this post published via the IoST Project can be found here.

«

This tutorial builds upon both the code base and circuitry as developed with the Particle.publish() and Particle.subscribe() example. This time […]