The Beginner's Arduino Solder-Free Kit comes with everything you need to create an MP3 player, but JJ took it a step further by adding a Vishay ambient light sensor to the mix.
From the box, we got the Adafruit music maker playing the music.
See related product
Let's create our own application where the played music will depend on ambient light. The VEML6030 from Vishay is used as the ambient light sensor to give us the number of lux. The data is obtained through an I2C bus.
See related product
Even if Arduino makes the programmation of the I2C bus very easy, it is necessary to understand the basics in order to program it.
The I2C bus is a standard widely used for sensors. It was designed by Philips (called NXP today) and works with only 2 lines : one line for the clock (called SCL) and one line for data (called SDA). This last line is bidirectional. These teo lines are open drain and need pull up resistors. These are the resistors you see on the breadboard. Obviously, on a final application, you design these resistors on your board. The master of the bus generates the clock and initiates communication with the slave. Each slave device has a 7-bit address.
See related product
The protocol is the following: the master sends a start bit (that is a high to low transition of SDA with SCL high) followed by the 7-bit address of the address it wished to communicate with, then followed by a single bit if it wishes to write (bit at 0) or to read (bit at one) from the slave. After each 8-bit transmission, the slave responds with an ACK bit (SDA is put low). If the transmitter sees a bit 1, it a NACK (not Acknowledged)
The end of the communication corresponds to a STOP bit. It is a low-to-high transition of SDA with SCL high. As Start and Stop bit occur when SCL high, all other transition of SDA take place with SCL low.
Now we understood how I2C works, we read the data in the datasheet of the VEML6030 to know the values to write in our program.
First, reading the schematic of the VEML6030, we can see an address pin allows configuring the address value. On our board, this pin is grounded, that means the address of the device is 0x10.
The datasheet explains the I2C interface and how to send commands. When we need to write to the device, we need to send the address, the command code. In the VEML6030, all the registers are 16 bits and the LSB must be first. So, we want to configure the VEML6030 with a gain of one quarter, integration time of 100ms and we need to power on the ambient light sensor, the LSB is equal to 0x00 and the MSB is equal to 0x18. In our program we defined the micros, include the wire library, in the setup, Wire.begin() is called, then in the loop function, we begin a transmission with the address of the VEML6030, the register command (0x00 in the present case) we send the LSB and MSB and finally and endTransmission. To have deeper explanation of these commands, you can go on Arduino web site where libraries are explained.
Similar way to read the data. we begin a transmission, send the register value where we want to read the data, requestFrom function with the address and 2 bytes to read and finally we read the data.
We convert the lux level in an integer and we finally decide which music to play depending on the lux value.
If below 20, we play a music to sleep. Between 20 and 50 lux, a music to relax, between 50 and 800, a music for an active life, and above 800, we image you are outside for a party and ready for techno music.
You are now ready to create your own app with any kind of sensors and customize your music player.