Gather Workshops Logo

Accelerator

Control motor speed via a touchpad

What We Are Making

A motor which will spin based on
a speed reading from a soft touch pad.

Bits We Need

  • Motor
    Motor
    The visual output for our circuit
  • Transistor
    Transistor
    To amplify the current to the motor
  • Diode
    Diode
    A one-way valve for the current
  • 330 Ohm Resistor
    330Ω Resistor
  • Soft Potentiometer
    Soft Potentiometer
  • 10k Resistor
    10k Resistor
  • Wire
    Wires
    To connect everything together!

Motor Overview

Setting up our motor will once again consist of two parts:
the physical components, and the logical components

Motor Diagram

Motor

The motor’s speed is determined by the voltage you put through it.

We send the motor an analog signal which is a number between 0 and 255.

The motor needs to be plugged into a digital pin which supports Pulse Width Modulation (PWM)

PWM allows us to send analog signals via a digital pin.

Transistor Diagram

Transistor

Diode Diagram

Diode

Physical Components
of the Motor Circuit

Motor wiring diagram

Set up your motor as in the diagram.

Logical Components
of the Motor Circuit

  • Test Buttons
    Test Buttons
    One for “off” and one for “on”.
  • Arduino Outputs
    Arduino Output
    Send the analog value to the motor.

Pass a Message Out to the Arduino

Motor Node Configuration

Drag an arduino out node into your workspace and configure it.

Inject a Message

Inject Node Configuration

Drag an inject node into your workspace and configure it.

Inject a Message

Inject Node Configuration

Drag an inject node into your workspace and configure it.

Connect the nodes

Inject Node Configuration

Click and drag the small square on the inject node,
and attach it to the arduino out node.

Deploy Your Code

Deploying Node Red code to the Arduino

Click the “Deploy” button in Node Red
to link your logic flow with the Arduino.

Test the Motor Output

The on-screen buttons should turn the motor on and off.

Potentiometer Overview

Setting up our potentiometer will also consist of two parts:
the physical components, and the logical components

Physical Components
of the Potentiometer Circuit

Potentiometer wiring diagram

Set up your potentiometer as in the diagram.

Logical Components
of the Potentiometer Circuit

  • Arduino In
    Potentiometer Input
    Converts the Arduino signal
    into a JavaScript message.
  • Debug Node
    Debug Logger
    Displays the JS message on
    the screen when received.

Receive a Message from the Arduino

Button Input Node Configuration

Drag an arduino in node into your workspace and configure it.

Debug the Incoming Data

Button Debug Node Configuration

Drag a debug node into your workspace. The default configuration is fine.

Connect the nodes

Join potentiometer Nodes

Join your arduino node to your debug node.

Deploy Your Code

Deploying Node Red code to the Arduino

Click the “Deploy” button in Node Red
to link your logic flow with the Arduino.

Test the potentiometer input

Turn the dial on your potentiometer.

You should see value being logged in your debug panel.

Logical Components
of the whole circuit

  • Arduino In
    Potentiometer Input
    Receives an Arduino signal
    as a JavaScript message.
  • Function
    Conversion Function
    Converts the potentiometer signal into the correct message for the LED.
  • Arduino Outputs
    LED Outputs
    Sends the JavaScript message
    as an Arduino signal.

Circuit Arduino Nodes

Full Circuit with all nodes

Link your arduino input to the three arduino output nodes
via a function, like we did last time.

This time your function requires three outputs instead of one.

Function Code

var pressurePoint = msg.payload;
var lowestSpeed = 130;
var highestSpeed = 255;
var maxPoint = 1024;

if(pressurePoint < 100) {
    msg.payload = 0;
} else {
  var percentTotalSpeed = pressurePoint / maxPoint;
  var speedRange = highestSpeed - lowestSpeed;
  var speedAboveMin = percentTotalSpeed * speedRange;
  var finalSpeed = lowestSpeed + speedAboveMin;

  msg.payload = finalSpeed;
}

return msg;

Paste this code into your function config popup,
in the code editor section.

Shorter Function Code

// mapping function
getSpeed = function(currentPressure) {
  var pressureMax = 1024;
  var motorMin = 130;
  var motorMax = 255;
  var speed = currentPressure * (motorMax - motorMin) / pressureMax + motorMin;

  return speed;
}

msg.payload = (msg.payload > 100) ? getSpeed(msg.payload) : 0;

return msg;

For experienced coders:
This code does exactly the same thing,
but is a bit shorter.

Deploy Your Code

Deploying Node Red code to the Arduino

Click the “Deploy” button in Node Red
to link your logic flow with the Arduino.

Full Circuit Test

Press different points on the soft potentiometer.

Your motor should spin at different speeds.

Thumbs Up!

Accelerator: Complete!

Well done :) That’s everything!

Loading...