Arduino research 17/05/2018

Today I’ve been looking at how to build the various Arduino circuits for the Space Rocks, and at some examples of relevant projects.

I made some basic sensor experiments with a Light-dependent resistor, ultimately using this code from the Make: Getting Started with Arduino book.

// Example 06b: Blink LED at a rate specified by the
// value of the analogue input

# define LED 9 // the pin for the LED

int val = 0; // variable used to store the value
 // coming from the sensor

void setup() {
 // put your setup code here, to run once:

pinMode(LED, OUTPUT); // LED is as an output

// note Analogue pins are
 // automatically set as inputs
 }

void loop() {
 // put your main code here, to run repeatedly:

val = analogRead(0); // read the value from the sensor

analogWrite(LED, val/4); // turn the LED on at
 // the brightness set
 // by the sensor

delay(10); // stop the program for some time

}

And this circuit:

LDR - LED circuit diagram
LDR – LED circuit diagram
LDR - LED circuit
LDR – LED circuit

Also stumbled upon How to Build an Arduino synthesizer with Mozzi library

The Mozzi library looks super-useful for sound generation:

Currently your Arduino can only beep like a microwave oven. Mozzi brings your Arduino to life by allowing it to produce much more complex and interesting growls, sweeps and chorusing atmospherics. These sounds can be quickly and easily constructed from familiar synthesis units like oscillators, delays, filters and envelopes.

You can use Mozzi to generate algorithmic music for an installation or performance, or make interactive sonifications of sensors, on a small, modular and super cheap Arduino, without the need for additional shields, message passing or external synths.

Note to self to also check out the Mozzi examples gallery.

Coding the Space Rock player with Arduino

Have had lots of fun and games trying to get the Arduino element of the player to work, so far.

Firstly, getting the newly-purchased SD card to read was relatively straightforward. Once I worked out that I could transfer the files through my camera (as I don’t have an SD card reader other than the one I had just bought to attach to the Arduino), testing to see if it was connected and reading the files on there worked okay. Once I had reformatted the card for FAT32, at least. There is still an issue with some of the files having names too long for the DOS 8.3 naming convention, but I will rename these later.

Testing the SD card reader
SD card reader working

Next, I bought the additional components (amplifier chip and capacitors) for, and built, this circuit:

Simple Audio player schematic
Simple Audio player schematic
Simple Audio player amplifier circuit

Unfortunately, not realising there was a difference between the Arduino DUE board, which this circuit was designed for, and the Arduino (Elegoo) UNO I have, took me off on the wrong track somewhat and cost me a fair bit of time. Essentially, the DUE uses the Audio.h library, which needs the DAC port. A port my UNO sadly doesn’t have.

So, next, lots of Googling, trying to find a simpler circuit and code that uses the TMRpcm library instead. For a while, every bit of code I verified was giving me errors similar to this one (I tried a few!):

So then I decided to try the other computer, and the same lines of code all worked fine.

Next, to build the circuit. I tried to build this:

Simple Audio player with two buttons.

But the wiring around the capacitors and amp chip lost me completely.

Then I started working with this code – https://maxoffsky.com/maxoffsky-blog/how-to-play-wav-audio-files-with-arduino-uno-and-microsd-card/ – and finally managed to play various of the .wav files on the card
(although the audio was so distorted it was sometimes hard to tell!).

But how to make the circuit, so that it wasn’t just plugged straight into the Arduino board? After a couple of false starts, I found and built this:

This meant I now had very distorted, but louder audio from the speaker.

Simple audio amp circuit for testing
Simple audio amp circuit for testing

So…next steps..?

  1. Build the more complex amplifier circuit above that works on the UNO, ideally with a potentiometer for volume control
  2. Find and write the code to play the .wavs whenever a tilt switch is activated, and turn off again when tilted back into place
  3. Find and write the code to make this happen randomly (ie play one of the eight .wav files at random) when the player is tilted forward (rather than the current keyboard entry in serial monitor)
  4. Last, but definitely not least, think about how the circuitry and power can fit inside the player shell

Watch this space.