Sound Generation

Hello, I want to generate sound with a 28byj-48 stepper motor, ULN2003 stepper driver, and an Arduino Mega. What I mean is, I want to make a note with the stepper motor and want to use the tone library (found here - Tone Library) to form music for the stepper motor. I don’t know how to code this, but here is my best shot:

//By UptownKitten453 and bhagman
//https://www.instructables.com/howto/uptownkitten453/
//https://github.com/bhagman/Tone

#include <Tone.h>

Tone SMPin[4];     //state how many pins there are


int notes[7] = { NOTE_A3,     //initialize the notes
                NOTE_B3,
                NOTE_C4,
                NOTE_D4,
                NOTE_E4,
                NOTE_F4,
                NOTE_G4 };

void setup() {
 
  SMPin[0].begin(8);   //initialize the pins
  SMPin[1].begin(10);
  SMPin[2].begin(9);
  SMPin[3].begin(11);
 
}

void loop() {

SMPin[0].play(notes[NOTE_A3]); //play a note
SMPin[1].play(notes[NOTE_A3]);
SMPin[2].play(notes[NOTE_A3]);
SMPin[3].play(notes[NOTE_A3]);

Schematic (lesson 30 (page 207)):

Schematic

All help will be appreciated!

Thanks,
UptownKitten

Also, is there some sort of calculation I can use to calculate the stepper motor's frequency of the buzzing noise?

Thanks,

UptownKitten

Also, is there some sort of calculation I can use to calculate the stepper motor's frequency of the buzzing noise?

Each step produces a click, if we call the time between steps T then the frequency you will here is equal to 1/T.

That code will not drive a stepping motor at all, that hardware is not suitable for using with the output of the tone library.

What you need is a stepping motor driver with a step and direction input. Then simply connect the step input to the pin you are generating the tone on.

Grumpy_Mike: Each step produces a click, if we call the time between steps T then the frequency you will here is equal to 1/T.

May you, please, elaborate on that? What do I use to code the stepper to produce a note? Is it only the step function? If I need to use the step function how do I know what frequency to use to produce a note? Do I need another driver, like the a4988 driver? groundFungus said that that person (groundFungus) has used the tone library before.

Link to that exact discussion: stepper tone library(#2)

What do I use to code the stepper to produce a note?

Anything you want, tone function, tone library or normal stepping motor code. You control the speed, this then gives you a frequency. I showed you the relationship between the time between steps and the frequency you will get the motor “singing at”.

What exactly do you mean by “step function”?

Do I need another driver, like the a4988 driver?

If you want to use a single trigger like a single output pin then yes. If you want the four output way of driving the motor you need just to control the speed of this to give you a controlled note.

The step function would be .step();.

I don’t understand, what I am trying to get at is how do I use the tone library with a stepper motor to produce music, like, what is the code?

UptownKitten: I don't understand, what I am trying to get at is how do I use the tone library with a stepper motor to produce music, like, what is the code?

What you need is a stepping motor driver with a step and direction input. Then simply connect the step input to the pin you are generating the tone on.

Hello, I got the EasyDriver and tested it out. The example code works, but the actual music code did not. What did I do wrong?

Example sketch:

const byte dirPin = 8;
const byte stepPin = 9;

void setup() {               
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  digitalWrite(dirPin, LOW);
  digitalWrite(stepPin, LOW);
}

void loop() {
  digitalWrite(stepPin, HIGH);
  delay(1);         
  digitalWrite(stepPin, LOW);
  delay(1);         
}

Actual music code:

//By UptownKitten453 and bhagman
//https://www.instructables.com/howto/uptownkitten453/
//https://github.com/bhagman/Tone

#include <Tone.h>

Tone SMPin[1];     //state how many pins there are


int notes[7] = { NOTE_A3,     //initialize the notes
                NOTE_B3,
                NOTE_C4,
                NOTE_D4,
                NOTE_E4,
                NOTE_F4,
                NOTE_G4 };

void setup() {
 
  SMPin[0].begin(9);   //initialize the pins
 
}

void loop() {

SMPin[0].play(notes[NOTE_A3]); //play a note
delay(1);
digitalWrite(9, LOW);
delay(1);
}

but the actual music code did not. What did I do wrong?

Difficult to say what you did right? You play a tone supposedly on pin 9 and then you follow it immediately by sending it a 500 Hz signal. At best it will swamp out the tone.

Note the library you used says

play() is non-blocking. Once called, play() will return immediately. If duration is given, the tone will play for that amount of time, and then stop automatically.

So as you give it no duration it will play again next time round the loop. This will reset the note and will not have time to make even half a cycle before it is reset again.

You need to remove the delays and digital write. Give the tone a duration and then use a loop so that you do not progress until the tone has finished. Then give it a 250 mS delay and let it loop round again.

What has happened to the direction pin? That input needs driving it is just it is not important what level it is but you must not let it float.