Hard Drive Solenoid Engine With Trinket


Solenoid engines are nothing new, though they still seem to be a fairly common project among the curious. Using a hard drive makes such a project much more simple and straight forward, that is if you want to keep it that way.

This solenoid engine uses a software control loop (code below) running on a 16MHz Adafruit Trinket to adjust its speed. The program specifically alternates between a modest 180RPM to a smashing 3000RPM every five or ten seconds! Watch the video below!

The connecting rod and crank shaft are 3D printed in PLA along with the IR interrupter flag and a couple spacer rings. With about two to three hours in operation at the time of this post, I have yet to see any significant wear or play in the joints. I’m hoping for some catastrophic failure at some point though, it should be fun to fix.


It can run on anywhere from 5 to 14 volts (partially a limitation of the Trinket’s regulator) and draws an average of a couple hundred milliamps on the low end, and up to an amp at its full voltage.

An IRF840 MOSFET controls the read head coil which in turn pulls the spindle during it’s optimum 90deg power stroke as measured by the Trinket with an IR slot sensor detecting the aforementioned 3D printed interrupter flag. Additionally, this flag can be rotated independently of the crank, or redesigned altogether to experiment with alternate timing patterns.

A small trim pot also allows fine tuning of of a governor feature in the code. This governor limits the speed of the engine by effectively narrowing the power stroke so less energy is exerted on the spindle/crankshaft.

One challenge of the coding procedure was the possibility that the spindle could rotate past the IR sensor before a control loop has finished. This would cause the motor to either run at “middle speeds”, or “backfire” by pulling the crankshaft from the wrong direction when throttling down from a higher speed. To solve this, the Trinket’s sole external interrupt feature was used to detect precisely when the flag has left the IR sensor and the motor was no longer in the proper part of its cycle. This alone was partly my reasoning behind this project; I wanted to become more familiar with Arduino’s attachInterrupts.

// Hard Drive Solenoid Engine With Trinket
// Visit thestuffwebuild.com for pictures and more.
// Photo interrupter on GPIO pin 2
// Debug LED on GPIO pin 1 (built into Trinket)
// MOSFET gate on GPIO pin 0
// Trim pot on GPIO pin 3

// Vars for hi/lo mode change
int governorOn = true; // start with governor on or not?
int wait = 10000; // how long to wait in mode above (ms)
unsigned long cm; 
unsigned long pm;

// Vars for governor
int govPotPin = 3; // trim pot pin for fine tuning
int governorLag;

// Slot sensor/photodetector input
int flagSense = 1; 

// Var for interrupt
int toolate = false;

void setup() {
  pinMode(1, OUTPUT); //led
  pinMode(0, OUTPUT); //mosfet

  // When the flag leaves the sensor, do "out();"
  attachInterrupt(0, out, RISING);

void loop() {
  // Run some checks
  while (analogRead(flagSense) <= 512 && toolate == false) {
    // Governor mode (coil is charged after a delay)
    if (governorOn == true){
      governorLag = map(analogRead(govPotPin), 0, 1024, 0, 80);
      if(toolate == false){
    // Full speed mode (coil is charged only when flag is in IR sensor)

  // Turn off coil
  if (analogRead(flagSense) > 512) {

  // Mode change timers
  cm = millis();
  if(cm - pm > wait) {
    pm = cm;
    if (governorOn == true){
      governorOn = false;
      wait = 5000;
      governorOn = true;
      wait = 10000;

// We turn on coil and LED for debugging
void on(){
  digitalWrite(0, HIGH); 
  digitalWrite(1, HIGH);
  toolate = true;

// We turn off coil and LED for debugging
void off(){
  digitalWrite(0, LOW); 
  digitalWrite(1, LOW);
  toolate = false;

// Interrupt code
void out(){
  toolate = true;

Now go and build your own with that box of scraps, it’s fun and easy; especially if you have an old hard drive laying around. I tried putting an output shaft on mine, but wasn’t too pleased with the design. Maybe you can do better.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.