Please select To the mobile version | Continue to access the desktop computer version
| |

Robot Forum

 Forgot password?
 Register
Search
View: 1026|Reply: 0

Sample code to make a DC motor play music

[Copy link]

16

Posts

0

Blogs

0

Albums

Administrator

Rank: 9Rank: 9Rank: 9

Credits
145
Post time: 2016-03-03 16:11:13
| Show all posts |Read mode
Musicical_Motor.jpg

It may surprise you to know that you can use electric motors as speakers. You simply need to drive the rotor back and forth at the right frequencies. The rotor will not spin at these frequencies but simply vibrate back and forth. The volume will depend greatly on the size of the motor and what is attached to the motor. It can be adjusted to some extent by varying the duty cycle.

I have attached some Arduino sample code that can be used with a Mini Driver. If you are using a motor shield then you will need to change the DIR and PWM (direction and power) pins to suit the shield you are using.

This sample code does not use any hardware timers so it will work on most Arduino boards and not conflict with any other libraries you might want to use. It simply determines the number of microseconds that the motor must go forward and backward for to produce a given note and drives the motor at 100% duty cycle.



Musical_Motors.zip

1.69 KB, Downloads: 399

Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

dagurobot

2017-11-23 00:27 GMT+8 , Processed in 0.248797 sec., 16 queries .

Powered by Discuz! X3.2

Release 20140618, © 2004-2017 Dagurobot

Quick Reply To Top Return to the list