Posts with «stepper motor» label

DIY Wire Bender Gets Wires All Bent Into Shape

It’s been a while since we’ve shown a DIY wire bending machine, and [How To Mechatronics] has come up with an elegant design with easy construction through the use of 3D-printed parts which handle most of the inherent complexity. This one also has a Z-axis so that you can produce 3D wire shapes. And as with all wire bending machines, it’s fun to watch it in action, which you can do in the video below along with seeing the step-by-step construction.

One nice feature is that he’s included a limit switch for automatically positioning the Z-axis when you first turn it on. It also uses a single 12 volt supply for all the motors, and the Arduino that acts as the brains. The 5 volts for the one servo motor is converted from 12 using an LM7805 voltage regulator. He’s also done a nice job packaging the Arduino, stepper motor driver boards, and the discrete components all onto a single custom surface mount PCB.

Wire straightener and feeder

The bender isn’t without some issues though, such as that there’s no automatic method for giving it bending instructions. You can write code for the steps into an Arduino sketch, which is really just a lot of copy and paste, and he’s also provided a manual mode. In manual mode, you give it simple commands from a serial terminal. However, it would be only one step more to get those same commands from a file, or perhaps even convert from G-code or some other format.

Another issue is that the wire straightener puts too much tension on the wire, preventing the feeder from being able to pull the wire along. One solution is to feed it pre-straightened wire, not too much to ask for since it’s really the bending we’re after. But fixing this problem outright could be as simple as changing two parts. For the feeder, the wire is pulled between copper pipe and a flat steel bearing, and we can’t help wondering whether perhaps replacing them with a knurled cylinder and a grooved one would work as the people at [PENSA] did with their DIWire which we wrote about back in 2012. Sadly, the blog entries we linked to no longer work but a search shows that their instructable is still up if you want to check out their feeder parts.

As for the applications, we can think of sculpting, fractal antennas, tracks for marble machines, and really anything which could use a wireframe for its structure. Ideas anyone?

Hack a Day 15 Oct 06:00

Stepper motor utilized as a rotary encoder with Arduino

Stepper motors work by alternating a series of magnets in order to rotate its shaft by a certain angle. When the shaft is manually twisted, these magnets produce an electrical signal in a predictable pattern, which as shown in the video below, can be used as an encoder with the help of an Arduino Uno.

More information, including a circuit diagram and the Arduino code used for the stepper-NeoPixel and stepper-stepper examples can be found here. While the write-up notes that this stepper-encoder won’t work reliably if turned too slowly, it seems to work quite well at the fairly low speed shown in the demonstrations.

I want to tell you how to make incremental encoder from stepper motor. When we turning shaft of stepper motor it works like generator. It generates certain impulses on its coils. After some signal processing, we get same impulses as incremental encoder. This encoder has one problem, it can drop steps if you turning very slowly. But for many applications, it doesn’t matter.

Watch The World Spin With The Earth Clock

With the June solstice right around the corner, it’s a perfect time to witness first hand the effects of Earth’s axial tilt on the day’s length above and beyond 60 degrees latitude. But if you can’t make it there, or otherwise prefer a more regular, less deprived sleep pattern, you can always resort to simulations to demonstrate the phenomenon. [SimonRob] for example built a clock with a real time rotating model of Earth to visualize its exposure to the sun over the year.

The daily rotating cycle, as well as Earth’s rotation within one year, are simulated with a hand painted plastic ball attached to a rotating axis and mounted on a rotating plate. The hand painting was done with a neat trick; placing printed slivers of an atlas inside the transparent orb to serve as guides. Movement for both axes are driven by a pair of stepper motors and a ring of LEDs in the same diameter as the Earth model is used to represent the Sun. You can of course wait a whole year to observe it all in real time, or then make use of a set of buttons that lets you fast forward and reverse time.

Earth’s rotation, and especially countering it, is a regular concept in astrophotography, so it’s a nice change of perspective to use it to look onto Earth itself from the outside. And who knows, if [SimonRob] ever feels like extending his clock with an aurora borealis simulation, he might find inspiration in this northern lights tracking light show.

This is a spectacular showpiece and a great project you can do with common tools already in your workshop. Once you’ve mastered earth, put on your machinists hat and give the solar system a try.

Hack a Day 18 Jun 16:31

Man-in-the-Middle Jog Pendant: Two Parts Make Easier Dev Work

In a project, repetitive tasks that break the flow of development work are incredibly tiresome and even simple automation can make a world of difference. [Simon Merrett] ran into exactly this while testing different stepper motors in a strain-wave gear project. The system that drives the motor accepts G-Code, but he got fed up with the overhead needed just to make a stepper rotate for a bit on demand. His solution? A grbl man-in-the-middle jog pendant that consists of not much more than a rotary encoder and an Arduino Nano. The unit dutifully passes through any commands received from a host controller, but if the encoder knob is turned it sends custom G-Code allowing [Simon] to dial in a bit acceleration-controlled motor rotation on demand. A brief demo video is below, which gives an idea of how much easier it is to focus on the nuts-and-bolts end of hardware when some simple motor movement is just a knob twist away.

[Simon]’s jog pendant moves a single motor which is exactly what he needs to ease development of his 3D printed strain-wave gear using a timing belt, but it could be programmed with any G-Code at all. Speaking of DIY jog pendants for CNC machines, don’t forget this wireless one made from an Atari 2600 joystick that jogs a plasma cutter in X and Y, and zeroes it with a push of the button.


Filed under: Arduino Hacks, cnc hacks

Modernizing a 170 year old Antique Grandfather Clock

Frankly, we let out a yelp of despair when we read this in the tip line “Antique Grandfather clock with Arduino insides“! But before you too roll your eyes, groan, or post snark, do check out [David Henshaw]’s amazing blog post on how he spent almost eight months working on the conversion.

Before you jump to any conclusions about his credentials, we must point out that [David] is an ace hacker who has been building electronic clocks for a long time. In this project, he takes the antique grandfather clock from 1847, and puts inside it a new movement built from Meccano pieces, stepper motors, hall sensors, LEDs, an Arduino and lots of breadboard and jumper wires while making sure that it still looks and sounds as close to the original as possible.

He starts off by building a custom electro-mechanical clock movement, and since he’s planning as he progresses, meccano, breadboard and jumper wires were the way to go. Hot glue helps preserve sanity by keeping all the jumper wires in place. To interface with all of the peripherals in the clock, he decided to use a bank of shift registers driven from a regular Arduino Uno. The more expensive DS3231 RTC module ensures better accuracy compared to the cheaper DS1307 or similar clones. A bank of RGB LEDs acts as an annunciator panel inside the clock to help provide various status indications. The mechanical movement itself went through several iterations to get the time display working with a smooth movement of the hands. Besides displaying time, [David] also added a moon phase indicator dial. A five-rod chime is struck using a stepper motor driven cam and a separate solenoid is used to pull and release three chime hammers simultaneously to generate the loud gong sounds.

And here’s the amazing part – he did all of this before laying his hands on the actual grandfather clock – which was shipped to him in California from an antique clock specialist in England and took two months to arrive. [David] ordered just the clock housing, dial/face and external parts, with none of the original inner mechanism. Once he received it, his custom clock-work assembly needed some more tweaking to get all the positions right for the various hands and dials. A clock like this without its typical “ticktock” sound would be pretty lame, so [David] used a pair of solenoids to provide the sound effect, with each one being turned on for a different duration to produce the characteristic ticktock.

At the end of eight months, the result – christened Judge – was pretty satisfying. Check the video below to judge the Judge for yourself. If you would like to see some more of [David]’s clockwork, check out Dottie the Flip Dot Clock and A Reel to Reel Clock.


Filed under: Arduino Hacks, clock hacks

TeensyStep – Fast Stepper Library for Teensy

The Teensy platform is very popular with hackers — and rightly so. Teensys are available in 8-bit and 32-bit versions, the hardware has a bread-board friendly footprint, there are a ton of Teensy libraries available, and they can also run standard Arduino libraries. Want to blink a lot of LED’s? At very fast update rates? How about MIDI? Or USB-HID devices? The Teensy can handle just about anything you throw at it. Driving motors is easy using the standard Arduino libraries such as Stepper, AccelStepper or Arduino Stepper Library.

But if you want to move multiple motors at high micro-stepping speeds, either independently or synchronously and without step loss, these standard libraries become bottlenecks. [Lutz Niggl]’s new TeensyStep fast stepper control library offers a great improvement in performance when driving steppers at high speed. It works with all of the Teensy 3.x boards, and is able to handle accelerated synchronous and independent moves of multiple motors at the high pulse rates required for micro-stepping drivers.

The library can be used to turn motors at up to 300,000 steps/sec which works out to an incredible 5625 rpm at 1/16 th micro-stepping. In the demo video below, you can see him push two motors at 160,000 steps/sec — that’s 3000 rpm — without the two arms colliding. Motors can be moved either independently or synchronously. Synchronous movement uses Bresenham’s line algorithm to plan motor movements based on start and end positions. While doing a synchronous move, it can also run other motors independently. The TeensyStep library uses two class objects. The Stepper class does not require any system resources other than 56 bytes of memory. The StepControl class requires one IntervallTimer and two channels of a FTM  (FlexTimer Module) timer. Since all supported Teensys implement four PIT timers and a FTM0 module with eight timer channels, the usage is limited to four StepControl objects existing at the same time. Check out [Lutz]’s project page for some performance figures.

As a comparison, check out Better Stepping with 8-bit Micros — this approach uses DMA channels as high-speed counters, with each count sending a pulse to the motor.

Thanks to [Paul Stoffregen] for tipping us off about this new library.


Filed under: Microcontrollers

Music Box Plays “Still Alive” Thanks to Automated Hole Puncher

Custom hole punch and feed system

Most projects have one or two significant aspects in which custom work or clever execution is showcased, but this Music Box Hole Punching Machine by [Josh Sheldon] and his roommate [Matt] is a delight on many levels. Not only was custom hardware made to automate punching holes in long spools of paper for feeding through a music box, but a software front end to process MIDI files means that in a way, this project is really a MIDI-to-hand-cranked-music-box converter. What a time to be alive.

The hole punch is an entirely custom-made assembly, and as [Josh] observes, making a reliable hole punch turns out to be extremely challenging. Plenty of trial and error was involved, and the project’s documentation as well as an overview video go into plenty of detail. Don’t miss the music box version of “Still Alive”, either. Both are embedded below.

As [Josh] mentioned on his project page, he was inspired by a tutorial video showing how to punch music by hand. It led to this tool to take a MIDI file and cut the music paper out on a laser cutter, whereas [Josh] and [Matt] were inspired to automate the entire process in their own way.

For those of you who don’t think science should stop there, why not automate the creation of the music itself with the output of this Bach-emulating Recurring Neural Network?

Thanks to [Tim Trzepacz] for giving us a heads up on this delightful project!


Filed under: musical hacks

Nextion Enhanced Stepper Motor Piano Project





Introduction


Nextion is a programmable human machine interface (HMI) that can be customized and designed to simplify the interaction between you and your project.
This Nextion Enhanced module (NX4827K043) with a resistive touch screen display, has some additional features not seen in previous traditional versions of the Nextion series.

  • A built in real time clock (RTC)
  • Accessible flash memory (32MB)
  • GPIO functionality
  • Faster clock speed

Before you connect the Nextion Enhanced module to your project, you need to design your interface with the free Nextion Editor. The editor can be downloaded here.
In this project, I will be designing a simple dynamic interface, which will allow me to interact with a stepper motor in two different ways.
The first interface will let me control the direction and speed of the stepper motor through the use of a simple GUI. I will have left and right arrows for the direction, and up and down arrows for the speed. I will also map the Expansion board to this interface for a more tactile experience.
The second interface will be more musical in nature. I will design a functional “Stepper motor piano” that will allow me to play simple songs using the rotational sounds of the stepper motor. This concept is not new, but I will show you how easy it is to make.

 
 

 
 

Project Scope


My project will show a splash screen when the project is powered up. After 3 seconds, the first interface will display.
The first interface will have 4 arrows:

  1. Left and Right arrows for the stepper motor direction of rotation
  2. Up and down arrows to increase/decrease stepper motor rotational speed
  3. Next Page button – to jump to the next interface

Each arrow/button on the first interface will be mapped to a specific button on the expansion board. Eg.

  1. Left/Right arrow is mapped to Left/Right button
  2. Up/Down arrow is mapped to Up/Down button
  3. Next page button is mapped to Enter button on the expansion board

The second interface will look like a piano on the Nextion Enhanced display. Each key on the piano will transmit a specific and unique number to the Arduino.
The specific number received by the Arduino will allow it to set the speed of stepper motor which will ultimately affect the frequency of sound it produces. Therefore when the “C” key is pressed on the Nextion display, the stepper motor will rotate at a frequency that sounds like a “C” note.



The stepper motor speeds can be determined by tuning the motor to specific notes using the first interface and an iPhone app called Tuner T1 Free".
If you plan to replicate this project, you will need to determine the relevant speeds of your own stepper motor, and substitute your values into the Arduino code later on in this tutorial.

 
 

Create a New Project


The first step is to create the interfaces in the Nextion Editor on your PC. You can download the Nextion Editor here.. Load up the Nextion Editor and create a new project.
When you start a new project, you need to make sure that you select the correct Nextion device from the available options.
I am using the “Nextion Enhanced NX4827K043” device.

  1. Select File → New
  2. Select a name for the project and save it to a suitable place on the hard drive
  3. Select the appropriate Nextion device from the available options
    1. My device has a screen size of 480 x 272 pixels



 
 

Project Resources


You need to import all of the resources (eg. pictures and fonts) into your project, and then design the interface to suit your specific needs.

Fonts


I will not be using any fonts in my project, but if you wanted to write any text to the display, you will need to generate a font in the Nextion Editor.

  1. Tools → Font Generator
    1. Select the Height of the Font (eg. 16)
    2. Select the Font code type (eg. iso-8859-2)
    3. Select if you want it to be in Bold or not
    4. Choose the Font you want to use (eg. Arial)
    5. Choose the spacing (eg. 0)
    6. And finally give this Font a unique name (e.g. Arial_16)
    7. Push the “Generate Font” button on the bottom right of the window



Once you press the Generate Font button, it will get you to save the font using a *.zi extension, and will automatically ask you if you would like to “Add the generated font?” to the project. If you are happy with the font, and would like to use this font in your project, then select “Yes”, otherwise select “No” and start again.


You cannot add any text to your project until you have imported or added a font. All of your project fonts will be displayed in the fonts window.


Each font will automatically be indexed, so that you can reference the font programmatically if required. In fact all resources that you add to your project are assigned a number and incremented by one for every resource added. For some resources, you can see this number to the left of the item. E.g. In the picture above, the Courier Font has an index of 0, whereas the Arial font has an index of 1. If you delete a resource, the index number may change for that item.

 

Pictures


As I said before, I will not be using any fonts for my project because the words on the screen will not be changing in any way. I can get away with designing a “Picture” and importing that into the project. I will need 3 pictures for my project.

  1. Splash screen
  2. Stepper Motor Controller
  3. Stepper Motor Piano

On the Nextion Enhanced NX4827K043 device, each picture must be

  • 480 x 272 pixels in size

We will now import the following pictures into the Nextion Editor so that we can use them in the project.
In the bottom left hand corner of the Nextion editor is the “Fonts and Picture” resource window:

  1. Select the Picture tab
  2. Then select the “+” icon
  3. This will open a dialog box to allow you to select the picture(s) to add to the project. You can select more than one picture to import.

I imported the following pictures from my computer:

 
 

Splash Screen





 
 

Interface 1: Stepper Motor Controller





 
 

Interface 2: Stepper Motor Piano




 
 

Creating the GUI



Pages


Every resource will get an ID based on the order it is added, and each resource will automatically get a name. You can change the name of the resource or object, but you cannot edit the ID.
Three pages will be designed to meet the criteria described above.
To add a page, you simply select the “Add” icon from the “page window”. And keep adding pages until you have a total of 3 pages (page0, page1 and page2).



 
 

Page 0 - Splash Screen


When the Nextion is powered up, the splash screen will be displayed for 3 seconds before it shows the Stepper Motor Controller screen. I used the following steps to create the splash screen.

  1. Add the splash screen picture to page0
    1. Select “page0” from the Page window
    2. Select “Picture” from the Toolbox window
    3. Double-click the “pic” attribute from the Attribute window
    4. Select the splash screen image from the list
    5. Press the OK button
  2. Add a Timer to page0
    1. Select Timer from the Toolbox window
    2. Change the “tim” attribute from 400 to 3000 in the Attribute window
    3. Enter “page page1” in the User code section of the Timer Event(0)

This timer event will make the Nextion jump to page1 after 3 seconds.



 
 

Page 1 - Stepper Motor Controller


This page is designed to control the direction and speed of the stepper motor.
There will be two buttons for the direction (Left and Right), and two buttons for the speed (Faster and Slower). And one more button to jump to the next page (i.e. the Stepper Motor Piano page). These buttons will also be mapped to the Nextion expansion board. The tactile buttons of the expansion board will provide an alternative method of controlling the motor.

  1. Add the Stepper Motor Controller picture to page1
    1. Select “page1” from the Page window
    2. Select “Picture” from the Toolbox window
    3. Double-click the “pic” attribute from the Attribute window
    4. Select the “Stepper Motor Controller” image from the list
    5. Press the OK button
  2. Add Hotspots over each button on the Stepper Motor Controller image
    1. Select “Hotspot” from the Toolbox window
    2. Drag and resize the Hotspot so that it covers the “Left” button
      1. This is the area that will respond to “Left button” presses.
      2. It will be transparent when uploaded to the Nextion board
    3. Select the “Touch Press Event” tab in the Event window
    4. Un-Check the “Send Component ID” checkbox
    5. Type the following code into the “User Code” Section of the Event window:
      • print “L”
    6. Change the object name of the hotspot to “Left” using the following process:
      1. Select objname from the attribute window and change the text from “m0” to “Left”
      2. It is not compulsory to change the hotspot object name; however it will help later on.
    7. Repeat steps 2a-2f for each of the other buttons in the following order and as per the table below
      1. Right
      2. Faster
      3. Slower
      4. Next


The decimal ASCII code for the letter “L” is 76, hence when the Nextion Enhanced display sends the letter L to the Arduino using the print “L” command, the Arduino will receive the number 76. When the right button is pressed, it will receive the number 82, and so on.
The “Next” button does not transmit anything to the Arduino, it is simply there to jump to the next interface on the Nextion Enhanced display, hence the reason why the user code is different for that button.
  1. Map the buttons to the Expansion board
    1. Select “page0” and then “page1” from the Page window


    2. Select the “Preinitialize Event” tab from the Event window
    3. Enter the following code into the “User Code” field of the Preinitialize Event tab:
      • cfgpio 5,1,Left
      • cfgpio 2,1,Right
      • cfgpio 4,1,Faster
      • cfgpio 3,1,Slower
      • cfgpio 1,1,Next


Please note: There is one space between cfgpio and the number next to it, but there are no other spaces on each line. If you introduce extra spaces, it will not compile.
This code maps the buttons on the expansion board to the hotspot objects on page1. For example, when the Left button (IO5) on the expansion board is pressed, it simulates the actions or events associated with hotspot m0/Left. In this case it will send a value of “L” (76) to the Arduino.


The IO number is marked within brackets on the expansion board.




 
 

Page 2 - Stepper Motor Piano


This interface will be designed to look like a piano, and will allow me to control the stepper motor such that it produces a note in the same key as the one I press on the Nextion display. The stepper motor will produce the note by rotating at a specific frequency.

  1. Add the Stepper Motor Piano picture to page2
    1. Select “page2” from the Page window
    2. Select “Picture” from the Toolbox window
    3. Double-click the “pic” attribute from the Attribute window
    4. Select the “Stepper Motor Piano” image from the list
    5. Press the OK button
  2. Add Hotspots over each key on the Stepper Motor Piano image
    1. Select “Hotspot” from the Toolbox window
    2. Drag and resize the Hotspot so that it covers the the “A” key.
      1. This is the area that will respond to “A-key” presses.
      2. It will be transparent when uploaded to the Nextion board
    3. Select the “Touch Press Event” tab in the Event window
    4. Type the following into the “User Code” section
      • print 1
    5. Repeat steps 2a-2d for each of the other keys as per the table below


When the specific key is pressed, the Nextion Enhanced board will transmit the printed number, followed by three 0x00 values. The terminating values can be ignored.


  1. The “Back” button will allow me to jump back to the previous interface on the Nextion Enhanced board.
    1. Create a hotspot for the back button using the following process:
      1. Select Hotspot from the Toolbox window
      2. Move/Resize the hotspot over the “Back” button
    2. Select the Event window
    3. Make sure the “Touch press event” tab is selected
    4. Type:   page page1   into the User Code section


 
 

Debugging


The good thing about the Nextion Editor, is that you can test out the interface functionality before uploading it to the board.



  1. Save the project by pressing the save button on the task bar
  2. Then press the compile button
  3. Then press the debug button.

A Nextion emulator window will appear. This window should respond in the same manner as Nextion module after the Nextion file is uploaded to the board. This emulator is a great way to test out your interface and to make sure it looks and works as expected. Once I was happy with the interface(s), I transferred the compiled Nextion file onto an SD card:

  1. Press the compile button
  2. File → Open Build Folder
  3. Select the *.tft file with the same name as that of the project
  4. Copy it to a micro SDHC card
  5. Insert the SDHC card into the SD card slot on the Nextion display
  6. Power up the Nextion board

Wait for the file to flash the Nextion board, and you should see a message that looks like this:


The next step is to power off the Nextion board, and remove the SDHC card.

 
 

ARDUINO SETUP


The Nextion Enhanced display is ready, and now it is the Arduino’s turn. The Arduino is programmed to receive Serial messages from the Nextion Enhanced display and control the stepper motor based on the letters or numbers received. The unique letters or numbers being transmitted from the Nextion board, allow the Arduino to understand what button is being pressed, and it uses those numbers or letters to control the flow of code in order to perform specific stepper motor actions.

 

Arduino Libraries and IDE


The Arduino IDE can be downloaded from this site.
The SoftwareSerial library is used to enable Serial communication between the Arduino and the Nextion Enhanced display.
The AccelStepper library is used to simplify the process of stepper motor control.

 
 

ARDUINO CODE:


Here is the Arduino Code for this project:
I set up a maximum and minimum speed for the motors, and some pre-defined keys. It is possible to “tune” the motor using the first interface of the Nextion display. You can do this by making the motor turn faster or slower until you reach the desired key.
I used the “Tuner T1 Free” app from the iTunes app store to identify WHEN the motor was producing a note in key.
When the motor was producing a specific note, I would write down the stepper motor speed that was printed to the Serial monitor window. Every time the motor speed is increased or decreased, the Arduino code prints the speed to the serial monitor window. I then use these speeds to update the notes[] array in the Arduino code.
The notes[] array holds the stepper motor speeds that correspond to the individual notes on the piano. The Nextion display essentially sends the index number of the note to play from the notes array on the Arduino, thereby simplifying the code required to spin the motor at 16 different speeds.

 
 

Hooking it up:


With all boards powered off, the next step is to make all of the necessary hardware connections to the Arduino. There are two major sections to consider,

  1. The Stepper motor driver and motor
  2. The Nextion Enhanced board

You need to ensure that you use an external power source to power both the stepper motor and the Nextion Enhanced board. The stepper motor driver board itself was powered by the Arduino without any problems, but the actual stepper motor will need an external power supply. The Nextion Enhanced board also needs an external power supply because it requires more current than the Arduino can safely provide.
Here is how you would connect the Arduino to the Stepper motor driver board and associated stepper motor.










 
 
And this is how you would connect the Arduino to the Nextion Enhanced display










 
 
And this is what it looked like when I put it all together:






Make note of the external power supply used. I made sure that I had a large enough power supply to handle the power requirements of the project, and utilized the relevant datasheets to help me identify those requirements. If you plan to replicate this project, make sure you take into consideration the specific power requirements of your motor, your motor driver and your Nextion display. The Arduino can only supply 400mA of current from the 5V pin.
With everything hooked up, I powered up the Nextion display, then powered up the Arduino. The stepper motor starts spinning automatically. I used the first interface to change the direction and/or speed of the motor. Please note the maximum and minimum speeds set up in the Arduino code.
I then used the Next button to jump to the second interface on the Nextion Enhanced display. The second interface looks like a piano. And when I press a key on the piano display, the motor changes speed to match the note I pressed.
Voila !! The stepper motor piano is born !!
I played a number of simple tunes on the Stepper motor piano and was surprised how well it worked. Very clever !!

 
 

Concluding comments


This project is relatively simple, but stepper motors can be tricky to set up and tune. Nothing a bit of determination cannot fix.
This project was a lot of fun. If you plan to replicate this project, I would be interested to see your versions, or just knowing if this helped you in any way.

 



This project would not have been possible without the collaborative efforts of iTead Studio. Their Nextion Enhanced display has a lot more to offer than what I have shown you here. But hopefully this tutorial gives you some insight into the power of such a display, and perhaps how it could improve the project you are currently working on. The only thing I did not like was the power requirements. I would have preferred something within Arduino power supply limits. Nevertheless, I am very happy with the Nextion Enhanced display, and would recommend it to anyone looking for a Human Machine interface to include in their project. You can see how simple it was to create TWO interfaces for my project, and I only scratched the surface.

Nextion Enhanced Stepper Motor Piano Project

Introduction

Nextion is a programmable human machine interface (HMI) that can be customized and designed to simplify the interaction between you and your project.

This Nextion Enhanced module (NX4827K043) with a resistive touch screen display, has some additional features not seen in previous traditional versions of the Nextion series.

  • A built in real time clock (RTC)
  • Accessible flash memory (32MB)
  • GPIO functionality
  • Faster clock speed

Before you connect the Nextion Enhanced module to your project, you need to design your interface with the free Nextion Editor. The editor can be downloaded here.

In this project, I will be designing a simple dynamic interface, which will allow me to interact with a stepper motor in two different ways.

The first interface will let me control the direction and speed of the stepper motor through the use of a simple GUI. I will have left and right arrows for the direction, and up and down arrows for the speed. I will also map the Expansion board to this interface for a more tactile experience.

The second interface will be more musical in nature. I will design a functional “Stepper motor piano” that will allow me to play simple songs using the rotational sounds of the stepper motor. This concept is not new, but I will show you how easy it is to make.


 
 
 
 

Project Scope

My project will show a splash screen when the project is powered up. After 3 seconds, the first interface will display.

The first interface will have 4 arrows:

  1. Left and Right arrows for the stepper motor direction of rotation
  2. Up and down arrows to increase/decrease stepper motor rotational speed
  3. Next Page button – to jump to the next interface

Each arrow/button on the first interface will be mapped to a specific button on the expansion board. Eg.

  1. Left/Right arrow is mapped to Left/Right button
  2. Up/Down arrow is mapped to Up/Down button
  3. Next page button is mapped to Enter button on the expansion board

The second interface will look like a piano on the Nextion Enhanced display. Each key on the piano will transmit a specific and unique number to the Arduino.

The specific number received by the Arduino will allow it to set the speed of stepper motor which will ultimately affect the frequency of sound it produces. Therefore when the “C” key is pressed on the Nextion display, the stepper motor will rotate at a frequency that sounds like a “C” note.


The stepper motor speeds can be determined by tuning the motor to specific notes using the first interface and an iPhone app called Tuner T1 Free".

If you plan to replicate this project, you will need to determine the relevant speeds of your own stepper motor, and substitute your values into the Arduino code later on in this tutorial.


 
 

Create a New Project

The first step is to create the interfaces in the Nextion Editor on your PC. You can download the Nextion Editor here.. Load up the Nextion Editor and create a new project.

When you start a new project, you need to make sure that you select the correct Nextion device from the available options.

I am using the “Nextion Enhanced NX4827K043” device.

  1. Select File → New
  2. Select a name for the project and save it to a suitable place on the hard drive
  3. Select the appropriate Nextion device from the available options
    1. My device has a screen size of 480 x 272 pixels


 
 

Project Resources

You need to import all of the resources (eg. pictures and fonts) into your project, and then design the interface to suit your specific needs.

Fonts

I will not be using any fonts in my project, but if you wanted to write any text to the display, you will need to generate a font in the Nextion Editor.

  1. Tools → Font Generator
    1. Select the Height of the Font (eg. 16)
    2. Select the Font code type (eg. iso-8859-2)
    3. Select if you want it to be in Bold or not
    4. Choose the Font you want to use (eg. Arial)
    5. Choose the spacing (eg. 0)
    6. And finally give this Font a unique name (e.g. Arial_16)
    7. Push the “Generate Font” button on the bottom right of the window

Once you press the Generate Font button, it will get you to save the font using a *.zi extension, and will automatically ask you if you would like to “Add the generated font?” to the project. If you are happy with the font, and would like to use this font in your project, then select “Yes”, otherwise select “No” and start again.

You cannot add any text to your project until you have imported or added a font. All of your project fonts will be displayed in the fonts window.

Each font will automatically be indexed, so that you can reference the font programmatically if required. In fact all resources that you add to your project are assigned a number and incremented by one for every resource added. For some resources, you can see this number to the left of the item. E.g. In the picture above, the Courier Font has an index of 0, whereas the Arial font has an index of 1. If you delete a resource, the index number may change for that item.


 

Pictures

As I said before, I will not be using any fonts for my project because the words on the screen will not be changing in any way. I can get away with designing a “Picture” and importing that into the project. I will need 3 pictures for my project.

  1. Splash screen
  2. Stepper Motor Controller
  3. Stepper Motor Piano

On the Nextion Enhanced NX4827K043 device, each picture must be

  • 480 x 272 pixels in size

We will now import the following pictures into the Nextion Editor so that we can use them in the project.

In the bottom left hand corner of the Nextion editor is the “Fonts and Picture” resource window:

  1. Select the Picture tab
  2. Then select the “+” icon
  3. This will open a dialog box to allow you to select the picture(s) to add to the project. You can select more than one picture to import.

I imported the following pictures from my computer:


 
 

Splash Screen


 
 

Interface 1: Stepper Motor Controller


 
 

Interface 2: Stepper Motor Piano


 
 

Creating the GUI

Pages

Every resource will get an ID based on the order it is added, and each resource will automatically get a name. You can change the name of the resource or object, but you cannot edit the ID.

Three pages will be designed to meet the criteria described above.

To add a page, you simply select the “Add” icon from the “page window”. And keep adding pages until you have a total of 3 pages (page0, page1 and page2).


 
 

Page 0 - Splash Screen

When the Nextion is powered up, the splash screen will be displayed for 3 seconds before it shows the Stepper Motor Controller screen. I used the following steps to create the splash screen.

  1. Add the splash screen picture to page0
    1. Select “page0” from the Page window
    2. Select “Picture” from the Toolbox window
    3. Double-click the “pic” attribute from the Attribute window
    4. Select the splash screen image from the list
    5. Press the OK button
  2. Add a Timer to page0
    1. Select Timer from the Toolbox window
    2. Change the “tim” attribute from 400 to 3000 in the Attribute window
    3. Enter “page page1” in the User code section of the Timer Event(0)

This timer event will make the Nextion jump to page1 after 3 seconds.


 
 

Page 1 - Stepper Motor Controller

This page is designed to control the direction and speed of the stepper motor.

There will be two buttons for the direction (Left and Right), and two buttons for the speed (Faster and Slower). And one more button to jump to the next page (i.e. the Stepper Motor Piano page). These buttons will also be mapped to the Nextion expansion board. The tactile buttons of the expansion board will provide an alternative method of controlling the motor.

  1. Add the Stepper Motor Controller picture to page1
    1. Select “page1” from the Page window
    2. Select “Picture” from the Toolbox window
    3. Double-click the “pic” attribute from the Attribute window
    4. Select the “Stepper Motor Controller” image from the list
    5. Press the OK button
  2. Add Hotspots over each button on the Stepper Motor Controller image
    1. Select “Hotspot” from the Toolbox window
    2. Drag and resize the Hotspot so that it covers the “Left” button
      1. This is the area that will respond to “Left button” presses.
      2. It will be transparent when uploaded to the Nextion board
    3. Select the “Touch Press Event” tab in the Event window
    4. Un-Check the “Send Component ID” checkbox
    5. Type the following code into the “User Code” Section of the Event window:
      • print “L”
    6. Change the object name of the hotspot to “Left” using the following process:
      1. Select objname from the attribute window and change the text from “m0” to “Left”
      2. It is not compulsory to change the hotspot object name; however it will help later on.
    7. Repeat steps 2a-2f for each of the other buttons in the following order and as per the table below
      1. Right
      2. Faster
      3. Slower
      4. Next

The decimal ASCII code for the letter “L” is 76, hence when the Nextion Enhanced display sends the letter L to the Arduino using the print “L” command, the Arduino will receive the number 76. When the right button is pressed, it will receive the number 82, and so on.

The “Next” button does not transmit anything to the Arduino, it is simply there to jump to the next interface on the Nextion Enhanced display, hence the reason why the user code is different for that button.

  1. Map the buttons to the Expansion board
    1. Select “page0” and then “page1” from the Page window
    2. Select the “Preinitialize Event” tab from the Event window
    3. Enter the following code into the “User Code” field of the Preinitialize Event tab:
      • cfgpio 5,1,Left
      • cfgpio 2,1,Right
      • cfgpio 4,1,Faster
      • cfgpio 3,1,Slower
      • cfgpio 1,1,Next

Please note: There is one space between cfgpio and the number next to it, but there are no other spaces on each line. If you introduce extra spaces, it will not compile.

This code maps the buttons on the expansion board to the hotspot objects on page1. For example, when the Left button (IO5) on the expansion board is pressed, it simulates the actions or events associated with hotspot m0/Left. In this case it will send a value of “L” (76) to the Arduino.

The IO number is marked within brackets on the expansion board.


 
 

Page 2 - Stepper Motor Piano

This interface will be designed to look like a piano, and will allow me to control the stepper motor such that it produces a note in the same key as the one I press on the Nextion display. The stepper motor will produce the note by rotating at a specific frequency.

  1. Add the Stepper Motor Piano picture to page2
    1. Select “page2” from the Page window
    2. Select “Picture” from the Toolbox window
    3. Double-click the “pic” attribute from the Attribute window
    4. Select the “Stepper Motor Piano” image from the list
    5. Press the OK button
  2. Add Hotspots over each key on the Stepper Motor Piano image
    1. Select “Hotspot” from the Toolbox window
    2. Drag and resize the Hotspot so that it covers the the “A” key.
      1. This is the area that will respond to “A-key” presses.
      2. It will be transparent when uploaded to the Nextion board
    3. Select the “Touch Press Event” tab in the Event window
    4. Type the following into the “User Code” section
      • print 1
    5. Repeat steps 2a-2d for each of the other keys as per the table below

When the specific key is pressed, the Nextion Enhanced board will transmit the printed number, followed by three 0x00 values. The terminating values can be ignored.

  1. The “Back” button will allow me to jump back to the previous interface on the Nextion Enhanced board.
    1. Create a hotspot for the back button using the following process:
      1. Select Hotspot from the Toolbox window
      2. Move/Resize the hotspot over the “Back” button
    2. Select the Event window
    3. Make sure the “Touch press event” tab is selected
    4. Type:   page page1   into the User Code section

 
 

Debugging

The good thing about the Nextion Editor, is that you can test out the interface functionality before uploading it to the board.

  1. Save the project by pressing the save button on the task bar
  2. Then press the compile button
  3. Then press the debug button.

A Nextion emulator window will appear. This window should respond in the same manner as Nextion module after the Nextion file is uploaded to the board. This emulator is a great way to test out your interface and to make sure it looks and works as expected. Once I was happy with the interface(s), I transferred the compiled Nextion file onto an SD card:

  1. Press the compile button
  2. File → Open Build Folder
  3. Select the *.tft file with the same name as that of the project
  4. Copy it to a micro SDHC card
  5. Insert the SDHC card into the SD card slot on the Nextion display
  6. Power up the Nextion board

Wait for the file to flash the Nextion board, and you should see a message that looks like this:

The next step is to power off the Nextion board, and remove the SDHC card.


 
 

ARDUINO SETUP

The Nextion Enhanced display is ready, and now it is the Arduino’s turn. The Arduino is programmed to receive Serial messages from the Nextion Enhanced display and control the stepper motor based on the letters or numbers received. The unique letters or numbers being transmitted from the Nextion board, allow the Arduino to understand what button is being pressed, and it uses those numbers or letters to control the flow of code in order to perform specific stepper motor actions.


 

Arduino Libraries and IDE

The Arduino IDE can be downloaded from this site.

The SoftwareSerial library is used to enable Serial communication between the Arduino and the Nextion Enhanced display.

The AccelStepper library is used to simplify the process of stepper motor control.


 
 

ARDUINO CODE:

Here is the Arduino Code for this project:

I set up a maximum and minimum speed for the motors, and some pre-defined keys. It is possible to “tune” the motor using the first interface of the Nextion display. You can do this by making the motor turn faster or slower until you reach the desired key.

I used the “Tuner T1 Free” app from the iTunes app store to identify WHEN the motor was producing a note in key.

When the motor was producing a specific note, I would write down the stepper motor speed that was printed to the Serial monitor window. Every time the motor speed is increased or decreased, the Arduino code prints the speed to the serial monitor window. I then use these speeds to update the notes[] array in the Arduino code.

The notes[] array holds the stepper motor speeds that correspond to the individual notes on the piano. The Nextion display essentially sends the index number of the note to play from the notes array on the Arduino, thereby simplifying the code required to spin the motor at 16 different speeds.


 
 

Hooking it up:

With all boards powered off, the next step is to make all of the necessary hardware connections to the Arduino. There are two major sections to consider,

  1. The Stepper motor driver and motor
  2. The Nextion Enhanced board

You need to ensure that you use an external power source to power both the stepper motor and the Nextion Enhanced board. The stepper motor driver board itself was powered by the Arduino without any problems, but the actual stepper motor will need an external power supply. The Nextion Enhanced board also needs an external power supply because it requires more current than the Arduino can safely provide.

Here is how you would connect the Arduino to the Stepper motor driver board and associated stepper motor.





 
 

And this is how you would connect the Arduino to the Nextion Enhanced display





 
 

And this is what it looked like when I put it all together:



Make note of the external power supply used. I made sure that I had a large enough power supply to handle the power requirements of the project, and utilized the relevant datasheets to help me identify those requirements. If you plan to replicate this project, make sure you take into consideration the specific power requirements of your motor, your motor driver and your Nextion display. The Arduino can only supply 400mA of current from the 5V pin.

With everything hooked up, I powered up the Nextion display, then powered up the Arduino. The stepper motor starts spinning automatically. I used the first interface to change the direction and/or speed of the motor. Please note the maximum and minimum speeds set up in the Arduino code.

I then used the Next button to jump to the second interface on the Nextion Enhanced display. The second interface looks like a piano. And when I press a key on the piano display, the motor changes speed to match the note I pressed.

Voila !! The stepper motor piano is born !!

I played a number of simple tunes on the Stepper motor piano and was surprised how well it worked. Very clever !!


 
 

Concluding comments

This project is relatively simple, but stepper motors can be tricky to set up and tune. Nothing a bit of determination cannot fix.

This project was a lot of fun. If you plan to replicate this project, I would be interested to see your versions, or just knowing if this helped you in any way.


 


This project would not have been possible without the collaborative efforts of iTead Studio. Their Nextion Enhanced display has a lot more to offer than what I have shown you here. But hopefully this tutorial gives you some insight into the power of such a display, and perhaps how it could improve the project you are currently working on. The only thing I did not like was the power requirements. I would have preferred something within Arduino power supply limits. Nevertheless, I am very happy with the Nextion Enhanced display, and would recommend it to anyone looking for a Human Machine interface to include in their project. You can see how simple it was to create TWO interfaces for my project, and I only scratched the surface.

A Command-Line Stepper Library with All the Frills

When you already know exactly where and how you’d like your motor to behave, a code-compile-flash-run-debug cycle can work just fine. But if you want to play around with a stepper motor, there’s nothing like a live interface. [BrendaEM]’s RDL is a generic stepper motor driver environment that you can flash into an Arduino. RDL talks to your computer or cell phone over serial, and can command a stepper-driver IC to move the motor in three modes: rotary, divisions of a circle, and linear. (Hence the acronumical name.) Best of all, the entire system is interactive. Have a peek at the video below.

The software has quite a range of capabilities. Typing “?” gets you a list of commands, typing “@” tells you where the motor thinks it is, and “h” moves the motor back to its home position. Rotating by turns, degrees, or to a particular position are simple. It can also read from an analog joystick, which will control the rotation speed forward and backward in real time.

Division mode carves the pie up into a number of slices, and the motor spins to these particular locations. Twelve, or sixty, divisions gives you a clock, for instance. Acceleration and deceleration profiles are built in, but tweakable. You can change microstepping on the fly, and tweak many parameters of the drive, and then save all of the results to EEPROM. If you’re playing around with a new motor, and don’t know how quickly it can accelerate, or what speeds it’s capable of, nothing beats playing around with it interactively.

Right now, there’s not much documentation aside from the code itself and the attached video, but actually that looks like all you’d need to get started. So if you’re looking to replicate Hackaday’s [Moritz Walter]’s excellent stepper-driver shootout, a tool like this is just the ticket.


Filed under: misc hacks