Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Midi to PWM to control Solenoids

  1. #1
    Junior Member
    Join Date
    Apr 2019

    Midi to PWM to control Solenoids

    Hey thanks for all the info already!

    I hope to be able to add to the community with this question regarding direction.....

    I have been reading and experimenting with controlling Solenoids (with no success) and Servo's (with some success) via midi send from a DAW (Ableton Live). I do not fully understand the language here, and I'm having trouble picking apart other concepts/tutorials in order to complete my puzzle...long and short of it I am on course to use midi signal as a trigger out of PWM pin on Teensy to hit a transistor gate that would then allow a higher power (about 18v) actuation of my solenoid...I have seen a bunch of similar schematics for this but not using midi in a "live" way, just something along the lines of "on for 1 second, off for 1 second"...or always just a bit different than what I am trying to there any direction you can point me in or clarity you can provide for the language being used? maybe an example script of what each command is doing?

    I would love to make a clarifying video with the information - it seems everyone who has a stake in this niche subject is very vague with the detail/explanation of how to communicate from Daw->Teensy->Solenoid.

    TLDR; I'd like to use midi notes sent from a DAW to control individual PWM pins on the Teensy that will in turn activate a Solenoid for automated percussion through a MOSFET transistor.

    Thanks for everything Paul & Community!!

    I have messed with this code to no avail....I am using a Teensy 2 so I know I'll have to change the PWM pins specified....
    /* / **************************************
    Pulse the PWM pins specified in pins[] when
    MIDI "note on" messages are received
    automatically turn off after set amount of time

    Use with 5 channel modular-muse motor driver board:
    Made for a Teensy LC

    or a 4 channel modular-muse motor driver board and some jumper wires

    NOTE: this code uses Teensyduino:

    pins[] - this array determines output pins to use (assumes PWM)
    lowNote - determines the starting MIDI note for the first motor channel, the rest count up one note per channel
    onTime - length time motor is pulsed on (in ms)
    MIDI velocity is mapped to PWM rate to control motor power

    by Jiffer Harriman -
    ************************************** */

    // use PWM pins 3, 4, 6, 9, 10
    int pins[] = {3, 4, 6, 9, 10};
    #define numNotes 5

    // motor channel 0 will be triggered by lowNote
    // motor channel 1 will be triggered by lowNote + 1 etc.
    int lowNote = 36; // MIDI note 60 (a.k.a. C-3)

    // how long to pulse in ms
    unsigned int onTime = 15;

    // keep track of the last time a motor channel (note) was triggered
    unsigned long lastHit[numNotes];

    // keep track if a note is currently on (high) or if it has been released
    bool noteHigh[numNotes];

    // Teensy MIDI callbacks
    void OnNoteOn(byte channel, byte note, byte velocity);
    void OnNoteOff(byte channel, byte note, byte velocity);

    void setup() {

    // initialize output pins to control the motors
    for (int i = 0; i < 5; i++) {
    pinMode(pins[i], OUTPUT);

    // no notes are on to start
    for (int i = 0; i < numNotes; i++) {
    noteHigh[i] = false;
    lastHit[i] = 0;

    // init MIDI callbacks

    // main loop
    void loop() {
    // flush the MIDI read buffer
    while ( {

    // for all motor driver pins
    // check if onTime since lastHit has elapsed
    for (int i = 0; i < numNotes; i++) {
    // if so, turn the motor off
    if (noteHigh[i] && (millis() - lastHit[i] > onTime)) {
    noteHigh[i] = false;
    analogWrite(pins[i], 0);

    // When a new "note on" message is recieved
    void OnNoteOn(byte channel, byte note, byte velocity) {

    // check if the note is in range of our possible motor outputs
    if (note >= lowNote && note < lowNote + numNotes) {
    // adjust for note # offset
    int noteNumber = note - lowNote;

    // bit shift 7 bits to 12
    analogWrite(pins[noteNumber], velocity << 5);

    // keep track of which note is high and reset the time since the lastHit to now
    noteHigh[noteNumber] = true;
    lastHit[noteNumber] = millis();


    // do nothing on "note off" messages since they are
    // automatically turned off after onTime
    void OnNoteOff(byte channel, byte note, byte velocity) {


  2. #2
    Any update on this ? I'm very interested !!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts