SchetsjesJS: moving the motors using Javascript

At the time of writing the first post I’ve already did steps 1-3 of my plan, leaving out one crucial element for a successful plotter: making it run on JavaScript. Step 4 of the plan was to check just that so let’s dive into that!

(Step 4:) See if we can move the motors using JavaScript

Turns out the motors I bought can be controlled with JavaScript! Very happy with this at the moment! I’ve found a module which seems to do exactly what I need: ‘motor-hat’ by jcane86. He or she comes from the UK, but I don’t have any more information.

What I’m surprised about is that there are a lot of modules controlling hardware via JavaScript but I have not seen a way to convert a module to JavaScript. I have some sensors laying around in my home, would be nice if I could make them work with JavaScript and contribute something to the community at the same time! If you’re reading this and you know how to do that: please point me in the right direction!

An Adafruit NEMA 17 stepper motor

One of my Adafruit NEMA 17 stepper motors. Amazing that this can be controlled using JavaScript!

Getting the module to work

Downloading and installing the module wasn’t hard at all, especially when you’re used to work with NPM. I’ve used the demo code from the Motor-hat API () and adjusted it a bit to answer a question I had. The stepper motors I bought are titled ‘Stepper motor – NEMA-17 size – 200 steps/rev, 12V 350mA’. Is the 200 the number of steps used to a full rotation?

“Error: , Remote I/O error”!
The code was ready, time to start it! Immediately ran into a problem with I/O: some kind of problem along the way with input and output. When testing the Motor HAT with the Adafruit example code I ran into a sentence that didn’t make sense to me at the time:

(…) You’ll always need to create an object, and set the address. By default the address is 0x60 (…)

This could be the solution! This sentence combined with something I found in the NPM Motor-hat API the solution presented itself: changing the address of the HAT to something called 0x60!

So, is the 200 the number of steps used to a full rotation? Turns out it is. Felt a bit of a rookie after finding out that the ‘rev’ in ‘200 steps/rev’ stood for ‘revolution’ and meant exactly that. I’d call it a rotation instead of a revolution, that got me a bit confused.

So, the code I used (modified example code):

var spec = {
address: 0x60,
steppers: [{ W1: 'M1', W2: 'M2' }]
}
var motorHat = require('motor-hat')(spec);
motorHat.init();
motorHat.steppers[0].setSteps(200);
motorHat.steppers[0].setSpeed({ rpm: 1 }); // revolutions per minute
motorHat.steppers[0].step('back', 200, (err, result) => {
if (err) {
return console.log('Oh no, there was an error', err);
}
console.log(`Did ${result.steps} steps ${result.dir} in ${result.duration / 1000} seconds. I had to retry ${result.retried} steps because you set me up quicker than your poor board can handle.`);
});

The result of this code was motor #1 making a 360 degree rotation in 60 seconds. Perfect! I don’t think time is something I want to use in my plotter logic but as far as tests go: this did exactly what I expected of it, pretty useful if you want to do something complex as building a plotter!

Heat 🔥

After the initial test I ran some other pieces of code on motor #1, leaving motor #2 untouched but connected to the HAT. After a while I wanted to move the motors a bit so I grabbed #1 and found out it was pretty hot to the touch. In my last blog post I made the following remark:

Everything seems to work and my home didn’t burn down, I’m satisfied!

It turns out you absolutely need to keep an airflow possible around the motors in order for them to lose heat. I had this but if you don’t there a serious risk of something overheating! I didn’t know that stepper motors use current while not rotating, good to know! The data sheet from Adafruit states that the temperature of this component is rated at a maximum of 80°C (rated current, 2 phase on). Not sure what the info between brackets means but ’80 degrees Celcius’ is something I know: that’s pretty hot! I may need to look into adding some cooling fans/ribs. For now I won’t let it out of my sight while being connected.

A table on which a Raspberry Pi a wooden board and a stepper motor are placed.

Next: building the plotter!

I cannot test any way of drawing without some kind of drawing board on which I can attach the motors, a sheet of paper, etc. The next step will be to mount the motors on some wood. But before I can do that I’ll need to buy some supplies! As you can see I’ve already started to work on it a bit. Combining electronics with wood is new to me: usually I keep these components sorted together and out of harms way. These components will have a different life, straight into the action because testing it without a real life situation is useless. I’m so excited!!!

 

Like to see how my plotter progresses? Read part three: SchetsjesJS: assembling a basic drawing board!