Posts tagged ‘Electronics’
More microscope images (21)
So I got a little carried away and started to take images of everything with my microscope:
Electronics
Materials
Eyes
Note the reflection in the image of my eye, if you zoom in and enhance you may be able to read what is on my screen which will then possibly lead to the solving of a murder.
I’ve wanted a USB microscope for a long time, and I have finally purchased a DigiMicro 200X Zooming USB Digital Microscope from dealextreme (product link) It only cost $40.23 USD (about £27.34 GBP) which I think is quite reasnoble for the fun and cool photos I’m getting out of it.
Quick Review
Advantages
- Very Cheap
- Easy to setup (plug and play)
- Can take some awesome photos, see below
- Three illumination settings (full on, half on, off) on top of software compensation
Disadvantages
- Controls (lights, zoom/focus and snapshot) are located on the body which make using them difficult if you are trying not to move your sample
- The stand is not very stable, tightening it to maximum holds it steady at certain angles for short periods of time
- Takes ~ 2 weeks to arrive from dealextreme as they are based in hong kong
Setup
The beauty of this webcam is that it requires no installation with ubuntu 9.10 you simply need to run the software and the camera works! woo! The output of `lsusb` shows the device as:
Bus 001 Device 006: ID 0c45:62e0 Microdia MSI Starcam Racer
and `dmesg` shows the following:
[22844.064666] uvcvideo: Found UVC 1.00 device USB 2.0 Camera (0c45:62e0) [22844.080844] input: USB 2.0 Camera as /devices/pci0000:00/0000:00:0b.1/usb1/1-6/1-6:1.0/input/input9
Software
Most video programs will pick up the device, I am opting to use a program called cheese which is awesome not only because of the name but also because of its simplicity.
sudo apt-get install cheese
Lens Cap
Remember to take the lens cap off otherwise you will end up focusing on the plastic cap as shown below. It is fairly obvious if this is the problem as moving the microscope will only change the light levels not the image.
Pretty images
Any requests for microscope images (within reason) will be considered
Python: interfacing with an arduino
So what is an arduino?
An arduino is an open source open hardware programmable controller with several inputs and outputs. The image below shows an Ardunio Dicemella.
It (Arduino Dicemella) has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.
They are very useful for people who know how to program but have little experience with hardware interaction.
Programming the arduino
This post will not contain in-depth detail on how to program the arduino, instead focussing briefly on setting up serial (over serial or usb cable) communications in order to talk to a python script. The arduino can be programmed via a IDE provided by the creators in a C-style hardware language.
Code example
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
}
}
Now we add a few lines to enable the writing of information from our arduino over the serial connection. We first need to set up the transfer speed in our setup (Serial.begin(9600);). Then we can simply send messages over serial using Serial.print(“message\n”);. You can choose between print and println with the difference been that the latter automatically appends the newline char, so we would use the former to write multiple things to the same line. Below is our modified code:
Serial write example
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
Serial.begin(9600);
Serial.print("Program Initiated\n");
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.print("LED Activated\n");
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
}
}
We now add into this code the ability to receive information via serial. Below is the modified example which removes the action of the button and replaces it by activating the LED when ‘Y’ is sent via serial.
Serial read example
int ledPin = 13; // choose the pin for the LED
int val = 0; // variable for reading the pin status
char msg = ' '; // variable to hold data from serial
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
Serial.begin(9600);
Serial.print("Program Initiated\n");
}
void loop(){
// While data is sent over serial assign it to the msg
while (Serial.available()>0){
msg=Serial.read();
}
// Turn LED on/off if we recieve 'Y'/'N' over serial
if (msg=='Y') {
digitalWrite(ledPin, HIGH); // turn LED ON
Serial.print("LED Activated\n");
msg=' ';
} else if (msg=='N') {
digitalWrite(ledPin, LOW); // turn LED OFF
}
}
Interaction with python
First we import the serial library to python in order to communicate with the arduino (this includes talking over usb).
import serial
We then attempt to connect to our arduino on /dev/ttyUSB0, using try and except to catch an exception if we are unable to find the arduino on USB0. The 9600 corresponds to the baud rate (speed of communication) that we are using with the arduino and should be the same as set in the program on the arduino otherwise your communication may appear garbled.
try:
arduino = serial.Serial('/dev/ttyUSB0', 9600)
except:
print "Failed to connect on /dev/ttyUSB0"
The address will be /dev/ttyUSB# where # is replaced by a number for arduinos connected via usb and /dev/ttyS# where # is replaced by a number for arduinos connected via serial. If you are not sure of the location of your arduino, it can be found in the arduino IDE or you can write some python to scroll through possible locations until a response is found
locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3', '/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3'] for device in locations: try: arduino = serial.Serial(device, 9600) except: print "Failed to connect on",device
You may need to be careful as other devices can be connected. For example if I try to connect to /dev/ttyS0 I will connect to the wacom tablet on my laptop.
Once you have connected to your arduino successfully you can write information to it using write and read information sent from it using read (you will need to import time to use the sleep function). If your arduino does not send any messages via serial then attempting to readline will result in your program hanging until it receives a message.
try:
arduino.write('Y')
time.sleep(1)
print arduino.readline()
except:
print "Failed to send!"
So the python code should now look like the following and we should be able to control the LED over serial.
import serial
import time
locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3',
'/dev/ttyS0','/dev/ttyS1','/dev/ttyS2','/dev/ttyS3']
for device in locations:
try:
print "Trying...",device
arduino = serial.Serial(device, 9600)
break
except:
print "Failed to connect on",device
try:
arduino.write('Y')
time.sleep(1)
print arduino.readline()
except:
print "Failed to send!"
The above will send the character ‘Y’ (Y for Yes please turn on the LED) to the arduino wait for 1 second and then read from the arduino which will have hopefully posted a response to our ‘Y’. Using the program on this should turn the LED on, and report LED Activated back via serial to our python program. This should be enough for people to get started with ardunios and communicating with them in python.
References
- Arduino – The arduino website with everything you are likely to need (programming examples and reference guide, and hardware information)
- Arduino tutorial – a basic and easy to understand tutorial on programming the arduino
- Python port of arduino-serial.c – By John Wiseman from which I based my program.
- original arduino-serial.c – by Tod E. Kurt.
- Sparkfun – Here is a good place to purchase ardunio and other electronics parts. Try coolcomponents if your from the uk like me
- Dealextreme – Hong Kong based retailer that sells a lot of cheap DIY electronics and also has worldwide free delivery with no min spend (crazy). Does take about two weeks to arrive though (uk).








































