Solenoids

Solenoids are basically electromagnets. Send current through it, and it becomes a magnet. Add an iron core in the shape of a piston, and you have a little actuator that pops the piston out/in when activated. Because they can be quite expensive, and you definitely will require a wall-wart transformer, your kit does not include it. They can be found at local electronics supply stores or online.

Depending on their size, solenoids draw a lot of current (>2A) when active, so bear that in mind if you want to keep a solenoid engaged for a long time. These can be use to push buttons, tap tuned plates, or pull very short distances (typically <2cm). There are also latching solenoids that ‘stick’ between the on/off state, those won't be good for tapping, but are meant for say fluid control valves.

In this recipe we are using the N-Drive MOSFET and a plain button to turn the solenoid on/off. Notice how the N-Drive MOSFET can be used to toggle various different high-powered circuits.

Notice how in this example we are using the 4xAA battery pack to power only the Photon; in this case 6V goes only to the Photon, and the solenoid itself is powered from the 12V/3A power supply via the MOSFET.

What type of sensor can you use to replace the button? For starters, basic sensors that report a simple ON/OFF state will be great. Examples include the PIR movement sensor, or the piezo vibration sensor.


Libraries Used

(learn how to import them in the Build IDE):


Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// code in this setup function runs just once, when Photon is powered up or reset
void setup() {
    Serial.begin(9600);             // Open a connection via the Serial port – useful for debugging

    pinMode(D0, OUTPUT);            // goes to N-Drive
    pinMode(D1, INPUT_PULLDOWN);    // goes to button

    delay(5000);                    // Common practice to allow board to 'settle' after connecting online
}


// code in this loop function runs forever, until you cut power!
// for the A/D blackbox, there is nothing much in here except to update the Blynk App
void loop() {
    updateSolenoid();
}

void updateSolenoid() {
    int sensorReading = digitalRead(D1);

    if(sensorReading==-HIGH) {
        digitalWrite(D0, HIGH);
    } else {
        digitalWrite(D0, LOW);
    }

    // add a tiny delay so as not to overwhelm the solenoid
    delay(10);
}