In this lesson, we will learn how to make a sound control lamp. The changing colors of RGB LED are controlled by the volume and pitch of the sound.
In this lesson, the color of the light needs to change with sound, so we use a RGB light that has been used in the previous lesson and a new component, microphone sensor to build our circuit.
Note: Turn over the microphone. By the way, the pin whose end is connected to the housing is cathode.
After creating the project, click Create variable button to create 5 variables, bluePin, grnPin, redPin, mic and micVal.
Initialize the pins in Start as shown below.
Create a Color function block to control the color of the RGB light and add three input name blocks; after that, input Red, Green, and Blue respectively.
In Raspberry Pi category, put set pwm P0 frequency to block and set pwm P0 pulse width to 0% block into Color block. Drag the redPin block from Variables category into the position as follows.
Get other two set pwm P0 pulse width to 0 % blocks and put them into the Color block. In addition, place grnPin block and bluePin block into proper position.
Find the Red, Green and Blue blocks from Variables category and put them into the set pwm P0 pulse width to 0 % blocks.
Read the value of microphone and assign it to the variable, micVal.
Print the value of microphone on the debug monitor.
Place an if do block under the print block, and click the setting button to add an else block below the if block as shown.
Put a Comparison Operator ( = ) block behind the if block and change “=” to “>”. Create a judgment condition by using micVal block and number ( 0 ) (change 0 to 120) block; if the value of microphone is greater than 120, execute the code in do.
Place a Color function block in do block, and find 3 random integer from 0 to 100 blocks in Math category then put them into the Color block. Accordingly, change the values as shown below.
Put another Color block in else block and set the three values to 0 to turn off the RGB LED.
Save the project and flash it into the Raspberry Pi (refer to lesson 0 for details). Now, the color of the RGB light will change with the rhythm of the music.
Find the matching cardboard of this lesson.
Fold flat cardboard into a cube according to the creases.
In the guidance of the tutorial, build the circuit as follows. Please do NOT change the position of components optionally when building circuits, in case the card cannot be installed.
Get the tube of cardboard mounted on the circuit, and the effect picture is as follows.