🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  • Arduino Lesson 10. Making Sounds
  • Overview
  • Parts
  • Playing a Scale
  • Sound
  • Pseudo-Theremin
  • Arduino Code
  • Other Things to Do
  1. Arduino Products
  2. Arduino Starter Kit

Lesson 10 Making Sounds

PreviousLesson 9. Sensing LightNextLesson 11 LCD Displays Part 1

Last updated 1 year ago

Arduino Lesson 10. Making Sounds

Created by Simon Monk

Last updated on 2022-12-01 01:52:48 PM EST

Table of Contents

Overview 3

Parts 3

  • Part

  • Qty

Playing a Scale 8

Sound 10

Pseudo-Theremin 11

Arduino Code 11

Other Things to Do 12

Overview

In this lesson, you will learn how to make sounds with your Arduino. First you will make the Arduino play a 'musical' scale and then combine this with a photocell, to make a Theremin-like instrument that changes the pitch played as you wave your hand over the photocell.

Parts

Part Qty

Piezo sounder 1

Half-size Breadboard 1

Arduino Uno R3 1

Jumper wire pack

1

Playing a Scale

For the first part of this lesson, the only thing on the breadboard is the Piezo buzzer. One pin of the piezo sounder goes to GND connection and the other to digital pin 12.

Program your Arduino with the following sketch:

/*

Adafruit Arduino - Lesson 10. Simple Sounds

*/

int speakerPin = 12; int numTones = 10;

int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};

// mid C C# D D# E F F# G G# A

void setup()

{

for (int i = 0; i < numTones; i++)

{

tone(speakerPin, tones[i]); delay(500);

}

noTone(speakerPin);

}

void loop()

{

}

To play a note of a particular pitch, you specify the frequency. See the following section on sound. The different frequencies for each note are kept in an array. An array is like a list. So, a scale can be played by playing each of the notes in the list in turn.

The 'for' loop will count from 0 to 9 using the variable 'i'. To get the frequency of the note to play at each step, we use 'tone[i]'. This means, the value in the 'tones' array at position 'i'. So, for example, 'tones[0]' is 261, 'tones[1]' is 277 etc.

The Arduino command 'tone' takes two parameters, the first is the pin to play the tone on and the second is the frequency of the tone to play.

When all the notes have been played, the 'noTone' command stops that pin playing any tone.

We could have put the tone playing code into 'loop' rather than 'setup', but frankly the same scale being played over and over again gets a bit tiresome. So in this case, 'loop' is empty.

To play the tune again, just press the reset button.

Sound

Sound waves are vibrations in the air pressure. The speed of the vibrations (cycles per second or Hertz) is what makes the pitch of the sound. The higher the frequency of the vibration, the higher the pitch.

Middle C is usually defined as a frequency of 261 Hz. If you turn a digital output on and off again 261 times every second then that output will be middle C.

To hear the output, we need to attach something that will convert the electrical signal into sound waves. This can be done with a loudspeaker or as we have used here a piezo sounder.

Piezo sounders use a special crystal that expands and contracts as an electrical signal passes through it. This will generate a tone that we can hear.

Pseudo-Theremin

We are going to make a similar instrument, albeit a lot less musical, but it will change the pitch of the note as you wave your hand in front of it.

We can leave the piezo sounder where it is, attached directly to the Arduino, but we will need the breadboard for the photocell and resistor that are going to control the pitch.

Arduino Code

Upload the following code on to your Arduino board.

/*

Adafruit Arduino - Lesson 10. Pseudo Thermin

*/

int speakerPin = 12; int photocellPin = 0;

void setup()

{

}

void loop()

{

int reading = analogRead(photocellPin); int pitch = 200 + reading / 4; tone(speakerPin, pitch);

}

The sketch is actually really straightforward. We simply take an analog reading from A0, to measure the light intensity. This value will be in the range of something like 0 to 700.

We add 200 to this raw value, to make 200 Hz the lowest frequency and simply add the reading divided by 4 to this value, to give us a range of around 200Hz to 370Hz.

Other Things to Do

Try changing the value 4 in the line below to lower and higher values.

int pitch = 200 + reading / 4;

This will expand or restrict the range of frequencies.

Returning to the first sketch, try and modify it to play a tune. Hint, you can just change the values in the 'tones' array. Note that if you change the number of notes from 10 notes, then you will need to change 'numTones' accordingly.

1 kΩ Resistor (brown, black, red stripes) 1

Photocell (Light Dependent Resistor) 1

The Theramin ( ()) is a musical instrument that makes spooky synthesized sounds as you wave your hands in front of it. It was used in the theme music for the original Star Trek series.

http://en.wikipedia.org/wiki/Theremin