Giving your Arduino projects ‘sight’ and ‘touch’: using IR, Photocells and Touch Sensors to your project

12419231_974683622601393_320731550894605287_oOK, so I am taking some liberty here with the terms sight and touch, but it got your attention, yes?

In this post, I am going to briefly share how to use three sensors: a ‘light detector’ or photocell, a touch sensitive ‘button’ and an IR receiver.

These three sensors all came from a company called ‘OSEPP’, but similar sensors can be had from other vendors as well.  I am going to write about these three specific sensors, but you should be able to adapt the information to what ever you have.  I will present code in both Tiny Basic and Arduino.

LIGHT SENSOR

• 3 pin outs: GRD (-) VCC (+) S ( Signal)

• Suitable supply voltage: +3 to 5Vdc
• Analog voltage output: 0 to 5 Vdc
• Detects ambient light density
• Works with CdsPhotoresistor
• Interface with microcontrollers and logic circuits • Analog sensors
• Uses PH 2.0 socket
• Special sensor with Arduino expansion boards

The light sensor is a cool photocell that is mounted on little breakout board, making it easier to use in a project. There are three pins: VCC, GND and Signal. Signal would connect to any of the Analog pins. When in use, it not only will let you know if it detects light, but also returns the intensity. The higher the value, the more light it detects. Reading it easy and you do not need any libraries. See the HalfByte Tiny Basic example below.

100 CLS
110 L=0: # PIN A0
120 P=13:# PIN 13 LED
130 A=AREAD(L)
140 IF A>299 DWRITE P, 0
150 IF A<300 DWRITE P, 1
160 CURSOR 0,0
170 ?”Intensity: “, A,” ”
180 DELAY 250
190 GOTO 130

What this little piece of code will do is turn on the LED if the light level drops below 300 and turns it off if it goes above 299. It also writes the level to the screen.

if you add a line, say 155, you can test for no light:
155 IF A=0 CLS: ?”NO LIGHT DETECTED”:DELAY 2000

Or, you can test for too much light:
155 IF A>=600 CLS:?”THE LIGHT IS TOO BRIGHT.”:DELAY 2000

There are many things you can do, for example, control a servo that turns an armature to open a food door to distribute dog food to a dish when the sun comes up.

You can use it to log when the sun comes, goes down. Use it control lighting, etc.

Here’s the Arduino sample:

/* OSEPP example of measured ambient light intensity from photocell .
depending on brightness, an LED brightness is changed.
more light = brighter LED. */

int photocellPin = A0;    // photocell sensor input
int ledPin = 11;      // select the pin for the LED
int photocellValue = 0;  // variable to store the value coming from the photocell val

void setup() {
Serial.begin(9600);

}

void loop() {
// read the value from the sensor:
photocellValue = analogRead(photocellPin); 
photocellValue = constrain(photocellValue, 200, 800); //adjust depending on environment.  
   
  // change brightness of LED depending on light intensity of photocell
  int ledbrightness = map(photocellValue, 200, 800, 0, 255);
 
  Serial.print(“incoming value from photocell sensor =”);
  Serial.println( photocellValue);
  analogWrite(ledPin, ledbrightness); 
  delay(100);       
}

TOUCH SENSOR

• 3 pin outs: G (GRD) V (VCC) S ( Signal)Basic wiring scheme for all three sensors.
• 3-5 V operating range
• 5 mA minimum current requirement.
• Capacitive touch detection

The touch sensor detects when you are touching the plate on the breakout board.  As long as you are touching the sensor, it returns a value.  You can test the value and determine if there is someone touching the sensor.

Arduino Code:

/*

OSEPP Touch Sensor Module Example

Serial monitor prints values output from touch sensor
when body conductivity applied.
*/

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600);
  // declare the ledPin as an OUTPUT:
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  Serial.println(“Touch Sensor Value = ” + sensorValue);

}

Tiny Basic Code:

100 CLS
110 L=0: # PIN A0
120 P=13:# PIN 13 LED
130 A=AREAD(L)
140 IF A>100 DWRITE P, 0
150 IF A<100 DWRITE P, 1
160 CURSOR 0,0
170 ?”Value: “, A,” ”
180 DELAY 250
190 GOTO 130

The Tiny Basic, very similar to the light sensor code, will turn the LED on and off each time you press the sensor.  On my setup, the values switch between 22 and 1023.  Your mileage may vary.

IR Sensor12792206_974716205931468_7006422127672690003_o

• 100% Arduino Compatible
• 3 pin outs: G –Ground V – 5V S – Signal
• Operates at a frequency of 38khz

The IR Sensor allows control of a circuit via an Infra Red remote or other IR source.  It works very much like the other two sensors here: has a ground, voltage and signal pins.  It returns values based on the IR signal received.  Admittedly, I have not yet done much with the sensor, so I have limited experience with it.  You can use the same Tiny Basic example from the Touch Sensor above.

Arduino Code:

/*
infrared sensor reciver. connect signal of infrared to analog pin 0. as the distance
from an object to sensor increases/decreases, you will increase/decrease
speed of led blinks from HIGH to LOW

*/

int IR_Pin = A0;    // select the input pin for the potentiometer
int IR_Value = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  IR_Value = analogRead(IR_Pin);   
 
Serial.println(“delay value for LED = “+ IR_Value);  //what value are we reading once an IR led is detected?

// IR_VALUE  = constrain(IR_VALUE, 0, 100); // optional to add a strict range
}

In each of the examples above, the sensors are connected to the HalfByte Console (or your Arduino compatible) via pin A0.  You can use what ever analog pins you want, just change the reference in the code.  You can use them together as well.

Getting your Raspberry Pi 2 up and running

WP_20150226_14_57_56_ProMy Raspberry PI 2 arrived today. Once again, it came in a really small package and just drives home the fact that these things are so small and, yet, so powerful.  And inexpensive. Forty plus dollars, including shipping.  This Pi is a quad core, 1 gb of RAM and nearly a gigahertz in speed.  It is six times faster than the original Pi and, yet, cost the same.  Remarkable.

Upon unboxing the unit, I created the NOOBS SD card and…had nothing but trouble from the start.  An interesting thing to note is that the SD cards you created for your original PI WILL NOT WORK with the Pi2.  Those had to be formatted using exFAT while this one uses FAT32. WP_20150226_20_24_49_ProThe instructions on the PI website are wrong.  Use a FAT32 formatted card.  I gave up on NOOBS and downloaded the Raspbian image and used Win32DiskImager to drop the image on the SD card. Doing so allowed the PI 2 to boot right up.  It is much faster booting than the original.

The included software, Python, Scratch, Wolfram Alpha, Python Games, Mindcraft and the Raspbian applets all start right up and work just fine.  I have yet to do much with it, it took a while to get it going and family time took precedence so I’ve had little time to WP_20150226_20_54_33_Proplay.  That is coming.

Just wanted to get my experience out there so others may have an easier time getting going.  Below are the summary steps I took.

  1. Format your SD card (Micro SD) using a FAT32 formatter
  2. Download the RASPBIAN Linux image from here.
  3. Use WinDiskImager32 to install the image on your SD Card.
  4. Insert the card in your PI 2 and boot it up.

That’s it.

Here are some more detailed instructions, but the four steps above will do the same thing.

Let us know how your experience went and stay tuned for more on the Pi 2.

Slow day at ZDNet? They don’t think the Raspberry Pi is a real computer

RaspPiBefore I begin my rant, please take a moment and read this post: How I spent almost 150 on a 35 computer.  Go on, I’ll wait.

Read it? Good.

OK, now on one level, Mr. Hess is correct: if you do not already have spare keyboards, mice, SD cards, etc., then, yes, the Pi WILL cost you more. BUT…on every other level, he wrong and wrong by a long shot.

First, lets get this out of the way: NO MATTER WHAT IT COST, it is still a computer. It fits every definition of a computer. It has input. It has output. It has a CPU. It has memory. It is programmable.  What it does not have, and the Foundation NEVER claimed that it did, are the PERIPHERALS that make it usable for humans.  The fact that it does not come with a monitor, keyboard or mouse does not disqualify it for a computer. Hell, if it did, the Apple Mac Mini would fail that definition as well. When I bought my Mini, I had spend almost another $100 JUST TO MAKE IT USABLE, and that did not include a monitor, which I already had. If I had to buy a monitor as well, the Mac Mini would have been nearly $300 more, at which case, I could have purchased a sweet Windows laptop. (Which, in hindsight, I should have done.)  So, if the Mini did not come with anything other than a power cord, does that disqualify it? No.

Now, Mr. Hess works in a very large datacenter with, presumably, some very large computers as well. I’m sure that not all of them have keyboards, mice and printers attached.  They very likely also lack monitors.  The datacenter in my former employer’s satellite office is full of computers that do not have anything other than network gear attached.  They are still computers.

Back to the Pi.

Yes, I will agree that you do need to spend more on it if you do not have everything. My Pi cost me right at a hundred dollars, but that is because I purchased a Motorola Atrix Laptop Dock and made the Pi a laptop. I also had to buy a special HDMI cable to connect it to the laptop dock. However, if I only used one of my small televisions, it would have cost me ten dollars more for the WiFi dongle I bought. I already had a few, but I wanted one of those tiny ones that do not stick out.  So, ten bucks more. I wanted the sharp HDMI display and integrated keyboard/mousepad that the Atrix Laptop Dock had, and I do not regret it.

And that dongle brings me to another one of Mr. Hess’ invalid points: the USB.  My Pi has a keyboard, mouse and WiFi and all are USB. To be fair, one of the Pi’s USB ports is taken up with the Laptop Dock, which includes two additional ports and the keyboard and mouse are built in, but are, nonetheless, USB. USB hubs have gotten very small and would work well on a Pi.  My desktop computer needed two hubs for all of its peripherals.

I have yet to acquire a computer that did not cost me more money a short time after purchase/acquisition. EVERY PC that I have purchased has resulted in a trip back to the store to purchase something additional.  Hell, the iPad cost me almost twice as much when you add in the extra power cable/charger, keyboard dock, camera kit, cases, Bluetooth keyboard,etc.  My Kindle Fire, which is not expandable, at all, still cost me extra since I bought a case and software.  My Asus Windows 8 tablet cost nearly a hundred bucks more since I had to buy a huge SD card and an external bluetooth keyboard.

I don’t know if Mr. Hess had nothing else to write about, or if ZDNet was just having a slow day, but this piece of drivel is just embarrassing for them.  Clearly, Mr. Hess does not ‘get it’.  The Pi and pretty much every other computer like it (including the awesome little Basic Stamp next to me right now) are for educational, hobby and other types of development. They are not meant to be used like a $299 computer you buy at Wal-Mart. Although the Pi is just as capable, though a bit on the slow side.

Ultimately, his post is his opinion and he is free to share it.  The problem, though, is that someone who may not know any better may not consider the Pi now because this man doesn’t think it is a computer. The Pi is perfect for young and old alike to learn the fantastic world of computing.  Once they are comfortable, they move on and pass the Pi to someone else.

I wonder what he thinks of the millions of computers you could buy in the late 1970’s through the very early ‘90s. Most of them lacked monitors, mass storage, some did not have keyboards and most did not even have a gui and, thusly, did not need a mouse.  I don’t know, I loved my TRS-80 Color COMPUTER.  All 32k bytes and 16 colors of it.

Oh, I almost forgot…appearing with Mr. Hess’ post…I saw this.

My PiTop is complete: Raspberry Pi and Motorola Atrix Lapdock together, at last

microplugsSo, I finally got the female to female micro-HDMI coupler that I needed to complete my Motorola Atrix Laptop project.  To recap, I purchased an Atrix Laptop dock (from Motorola) for my Raspberry Pi. The Raspberry Pi outputs video to either an HDMI cable (full size) or a crappy composite video connector (old-style RCA plug) and audio via headphone jack if using composite video.  Since the dock accepts HDMI and USB, it should be fairly easy to mate these two together for some kind of frankentop device.

Know what? It was easy.  Buuut…..getting the necessary connectors…well, that was quite the exercise.

Since both plugs on the dock were male, that meant I needed FEMALE couplers to plug the male cable into.  Finding these was just terrible. Seems that they are very niche products and I had to order them both, one on eBay and one on Amazon.

First, the easy one…I just ordered the Atrix cable kit. That comes with the USB cable I needed. I ordered from Amazon.

The second, though, I went through eBay and that came from China. Took nearly three weeks to ship!

At any rate, I got all of the necessary connectors, cables, wire ties, shrink tubing, etc. to finish the job.  Actually, just getting that damned micro-HDMI female coupler was the hardest part (and not burning my fingers with the soldering iron.)

NOTE: it is worth twisting the wires together and trying the cables first, before soldering. I did this to save myself any possible hassle should something not be right. However, when you solder them together, it is best NOT to twist them. Lay them in parallel and solder. Pre-tinning helps tremendously.

usbwiringSince I wanted to use the lapdock to power the Pi AND take advantage of the keyboard and mouse pad, I had to Frankenstein the USB cables too.  This is fairly easy: you want power only to the micro USB connector on the Pi, and DATA ONLY to the USB port on the Pi.  So…I had to cut the ends off of a full size male USB cable and cut the Atrix USB cable in half.  Carefully stripping away the outer tubing on each piece of cable, I then stripped the RED and BLACK wires on the female micro USB cable and the male micro-USB cable. These get some tubing before soldering. Once tubing is in place, carefully solder the wires. DON’T FORGET GROUND! Solder the ground wire as well.

Next, strip the WHITE and GREEN wires (the BLUE wire on the female side is not used here), place some tubing on each and solder. Next, pull the tubing over the expose wire, apply heat and set aside for a minute or two, the cable will be hot.  I used a large piece of shrink tubing on the outside, but it did not cover the actually spliced area, so…embarrassingly, I used electrical tape where the three cable ends meet. It is not pretty, but it does not have to be pretty, just needs to work.  I gave up my soldering days long ago, I pull the old iron out once in a while for repairs, not for looks.

Next, plug it all together and open the lid on the lapdock. If everything is ok, you will see your Pi booting on the gorgeous screen. Moto did a nice job selecting the screen, it looks great.

Now, after having used this set up for a few days, it is not all roses.  My biggest complaint: the damned trackpad. It does not take much to make the mouse move and you can find yourself typing where you didn’t want.  Also, shutting the lid will turn off and then turn on power to the Pi. Don’t know why, but it does.  USB can be problematic: typing and using a WiFi adaptor seem to cause a problem: the keyboard will slow. WiFi is also interefered with by the USB hub built into the lapdock. Sometimes, it gets unresponsive, un plugging and plugging back in the WiFi adaptor seems to fix it.

These little quirks aside, this combo is pretty cool. My PiTop works pretty well and it was cheap: less than a hundred bucks for everything. And the Pi is just cool…no matter what one does with it.