Friday, 4 August 2017

DIY Spectrometer

Have you ever wanted to see what the stars are made of, what is in a light source or even what is inside of your drinking water? Well, there is a tool for that in Star Trek it is called a tricorder but in the real world, we use a tool called a spectrometer. A spectrometer is a device that can analyse what is inside of an object based on the light that passes through that object.

How a Spectrometer Works

There are many different components that make up a spectrometer:
The sample/light source: This is what you are analysing

The slit: This allows only a sliver of light to pass through to be analysed.

The grating: It is similar to a prism where it splits the light into the different colours that make up that light.

The recorder: All this does is collect and capture the data provided from when the light is split by the grating.

When you put it all together it should work like this.


The setup:

Shine the light towards the spectrometer.
Place sample in between light and spectrometer (Optional)

What happens:

The light will pass through the slit allowing a narrow beam of light to pass through.
It will hit the grating at a 45-degree angle and be split into a spectrum.
The webcam will record that data and send it to the computer.


How to build a spectrometer:

The first step is to get an enclosure that is not too big but has the length width and height to fit your web camera.Next, you are going to need to completely fill it with black paper or cover every surface with black ink.

Then you will cut out a square where you can place the slit. To make the slit what you need to do is put two razor blades together then split them using a piece of paper. tape them down to a piece of black cardboard then once you've made the slit cut the piece of paper where the separation is to allow light to pass through. Go to 1:22 in the video to see how.
https://www.youtube.com/watch?v=fl42pnUbCCA

After that get a piece of grating. To make grating what you need to do is take an old CD and cut around the outer and inner edges of the CD. Then place tape all over the CD then peel it off. With the purple coloured disk cut that into a tiny rectangle that will fit on your webcam. Go to 3:03 in the video to see how.
https://www.youtube.com/watch?v=fl42pnUbCCA

Next, tape the grating to the webcam. Then place the webcam at a 45-degree angle to the slit and make sure the lens is perfectly lined up and that is a working spectrometer.

The Software

I have written two codes that work together to capture and analyze the data collected by the spectrometer.

What this code does is it captures the image. The image will pop up on the screen. In that image, we will take the Y value and input that into our next code

import numpy as np
import matplotlib.pyplot as plt
import cv2

cap = cv2.VideoCapture(1)
ret, frame = cap.read()

fig = plt.figure()
myax = plt.imshow(frame)
plt.show()

What this code will do is using the Y value from the image it will plot that row. It will plot it so that it is the intensity compared to the pixel position of that row.

import numpy as np
import matplotlib.pyplot as plt
import cv2

cap = cv2.VideoCapture(1)
ret, frame = cap.read()

spec = np.empty((0))
for x in range(1280):
    [r,g,b]=frame[305,x]
    #print x
    intensity = (int(r)+int(g)+int(b))/3.0
    spec = np.append(spec, intensity)

# Subtract minimum of spectrum
spec = spec - min(spec)

plt.plot(spec)
plt.show()








Wednesday, 12 July 2017

Updated version of the drum machine: Drum machine 3.0 (Even more noise, even more portable.)

If you have read my blog before you will know about the wearable drum machine that I made previously. This year I decided to make the project true to its name. I created a drum machine that was literally on the T-shirt itself. (Picture below.) As you can see it uses four sensors. Of course, there are still limitations because currently, you require the internet connection to run the drum synthesizer.

Major new features compared to gen. 2 project:

PHONE

This year I decided that since it had to be attached to a laptop it would not be deemed wearable. So this year I decided I had to make it fully portable so it now uses an adapter to connect the Arduino to a synthesizer on my phone.


How it works.

The hardware 

The FSR when tapped allows the current to pass through.
The current passes through the copper tape to the crocodile clips.
The crocodile clips connect to the wires which are attached to the Arduino. (To learn how to wire look at the link below.)
The Arduino through an adapter connects to a phone which has a drum synthesizer which activates based on keyboard commands.

The software

The Arduino is acting as a keyboard. Each time the Arduino receives a signal from one of the FSR it produces a keyboard command.
The Synthesizer relies on keyboard commands. For example, if I were to press the button on the keyboard 'F' it would make the sound of cymbals now when you press the FSR on the shirt it would do the exact same thing.


The brains of the project (Arduino)

Connect the FSR to the Arduino


To help you with connecting the FSR to the Arduino I have provided a link. In this link it shows a diagram showing the where to connect the FSR and what type of resistor you require to do so. To understand how FSR work you can do a quick and easy test by also adding an Led into the circuit and seeing how based on how hard you press the led for instance you can make the LED brighter and brighter. If you scroll further down it shows you multiple example codes.

Make it print out keyboard commands when pressed
An important part of the project is the synthesizer. This part of the project remains the same. It uses an online drum machine synthesizer that requires keyboard commands. This way whenever you touch an FSR it produces a keyboard command which gets sent to the synthesizer and produces the sound. The key component of making keyboard commands comes from the Arduino Leonardo which is the only Arduino with the capability of emulating a keyboard or a mouse. To look at how to make keyboard commands using the Arduino Leonardo you can look at the code below or take a look at my older version of the blog. In that, it shows the code for using readings to print keyboard commands.

/* Modified FSR code and USB keyboard code */

#include "Keyboard.h"

int fsrAnalogPin0 = 0; // FSR is connected to analog 0
int fsrAnalogPin1 = 1;
int fsrAnalogPin2 = 2;
//int LEDpin = 11;      // connect Red LED to pin 11 (PWM pin)
int fsrReading0;      // the analog reading from the FSR resistor divider
int fsrReading1;
int fsrReading2;
int pressForce = 20;
int LEDbrightness;

void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
//  pinMode(LEDpin, OUTPUT);
  Keyboard.begin(); // initialize control over keyboard
}

void loop(void) {
  fsrReading0 = analogRead(fsrAnalogPin0);
  delay(50);
  fsrReading1 = analogRead(fsrAnalogPin1);
  delay(50);
  fsrReading2 = analogRead(fsrAnalogPin2);
  delay(50);
  //Serial.print("Analog reading = ");
  //Serial.println(fsrReading);

  if (fsrReading0 > pressForce) {
    Keyboard.print("e");
  }
  if (fsrReading1 > pressForce) {
    Keyboard.print("f");
  }
  if (fsrReading2 > pressForce) {
    Keyboard.print("v");
  }

  // we'll need to change the range from the analog reading (0-1023) down to the range
  // used by analogWrite (0-255) with map!
  // LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
  // LED gets brighter the harder you press
  // analogWrite(LEDpin, LEDbrightness);

  delay(50);
}