“Sound” motor control @adafruit with CRICKIT #CRICKIT
“Sound” motor control (video). The demo is how to control the speed of a motor with sound using the microphone sensor on CircuitPlayground & CRICKIT. To everything (turn, turn, turn). Code & guide. Crickit – A Creative Robotics & Interactive Construction Kit. It’s an add-on to our popular Circuit Playground Express that lets you #MakeRobotFriend using CircuitPython, MakeCode, Arduino, etc.. robotics, arts, crafts, audio animatronics, sensors, agriculture/robot farming, physical computing, kinetic sculptures, science experiments, telescope control…
The Crickit is powered by seesaw, an I2C-to-whatever bridge firmware. So you only need to use two data pins to control the huge number of inputs and outputs on the Crickit. All those timers, PWMs, sensors are offloaded to the co-processor.
from busio import I2C
from adafruit_seesaw.seesawimport Seesaw
from adafruit_seesaw.pwmoutimport PWMOut
from adafruit_motor import motor
import audiobusio
importtimeimport board
importarrayimportmathprint("Sound sensor motor!")# Create seesaw object
i2c = I2C(board.SCL, board.SDA)
seesaw = Seesaw(i2c)# Create one motor on seesaw PWM pins 22 & 23
motor_a = motor.DCMotor(PWMOut(seesaw,22), PWMOut(seesaw,23))
motor_a.throttle=0# motor is stopped##################### helpers# Maps a number from one range to another.def map_range(x, in_min, in_max, out_min, out_max):
mapped =(x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
if out_min <= out_max:
returnmax(min(mapped, out_max), out_min)returnmin(max(mapped, out_max), out_min)# Returns the averagedef mean(values):
returnsum(values) / len(values)# Audio root-mean-squaredef normalized_rms(values):
minbuf =int(mean(values))returnmath.sqrt(sum(float(sample-minbuf)*(sample-minbuf)for sample in values) / len(values))# Our microphone
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth =16)
samples =array.array('H',[0] * 200)
mic.record(samples,len(samples))whileTrue:
mic.record(samples,len(samples))
magnitude = normalized_rms(samples)print(((magnitude),))
motor_a.throttle= map_range(magnitude,90,200,0,1.0)time.sleep(0.1)
from busio import I2C
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.pwmout import PWMOut
from adafruit_motor import motor
import audiobusio
import time
import board
import array
import math
print("Sound sensor motor!")
# Create seesaw object
i2c = I2C(board.SCL, board.SDA)
seesaw = Seesaw(i2c)
# Create one motor on seesaw PWM pins 22 & 23
motor_a = motor.DCMotor(PWMOut(seesaw, 22), PWMOut(seesaw, 23))
motor_a.throttle = 0 # motor is stopped
##################### helpers
# Maps a number from one range to another.
def map_range(x, in_min, in_max, out_min, out_max):
mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min
if out_min <= out_max:
return max(min(mapped, out_max), out_min)
return min(max(mapped, out_max), out_min)
# Returns the average
def mean(values):
return sum(values) / len(values)
# Audio root-mean-square
def normalized_rms(values):
minbuf = int(mean(values))
return math.sqrt(sum(float(sample-minbuf)*(sample-minbuf) for sample in values) / len(values))
# Our microphone
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth = 16)
samples = array.array('H', [0] * 200)
mic.record(samples, len(samples))
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print(((magnitude),))
motor_a.throttle = map_range(magnitude, 90, 200, 0, 1.0)
time.sleep(0.1)
Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards
Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.
Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7pm ET! To join, head over to YouTube and check out the show’s live chat – we’ll post the link there.
Python for Microcontrollers — Python on Microcontrollers Newsletter: Pi Day, GitHub 2FA and much more! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi