Wednesday, 31 December 2014

Repurposing Security Motion Detector for use with Raspberry Pi

Update:
I ended up removing the internal resistor and using two external resistors to hold it down to ground so the input didn't float. Also note that the motion is OPEN when there motion is detected (which makes sense, right?). Here is a schematic for something else I am working on, but shows how I attached a motion and a door contact (among other things) to the GPIO of a Pi. Please also note that the Aurora is being replaced, however, the new Honeywell version will likely be indistinguishably different from the installers perspective.


Add motion detection to your project using an off the shelf security system motion detector!
This example is not limited to just the Raspberry Pi, will also work with your 5V projects (recommend 4.7K resistor instead if you do this).


This motion detector is a quality component used for security system installations, this ‘Aurora’ model is made by Honeywell/Ademco and can be found new in the $15-25 range.



These hardwired models age well if you're considering re-purposing one; this one has been around for well over ten years and still works great. Bosch has a slick looking model too. Worth noting, you can see inside attached to one of the signal lines is a 1/2 watt resistor, (200 ohm I believe for that panel) - these are used to produce current so that the security panel can see if there is trouble, or if someone tries to short the wires out along the line it will detect the tamper.

On the inside of this model you will see 4 screw terminals. Two of these are to power the unit, the other two are for the signal. To power up the unit, the data sheet says

its operating characteristics as 8-16VDC with a form A SPST 90mA @16VDC relay. I used a 12VDC power supply to play with this. If you are using a Pi or an Arduino, you will need access to that 8-16VDC to power it. I performed measurements  on both the power input as well as the relay signal contacts.


On 12VDC supply lines:
No motion current: 3.82mA
Motion current: 9.38mA

Resistance on relay contact terminals:
No motion: 16 ohms
Motion: Infinite
Relay is closed while unit is powered and no motion detected, it opens if no power is present, or if motion is detected.

If you choose to disable the internal LED I would imagine you could shave off a few more mA if you're concerned about consumption.


To make the motion suitable for our Raspberry Pi, I attached a ¼ watt resistor with a value of 3.3K ohms onto the relay terminals; The current used by this device will now use <= 1mA with the 3.3V supply voltage of the pi, which will be suitable for our demonstration.

This picture to the right is with the new wire installed, which I used 22/4 station wire (standard for telephones and security systems). I also attached the new resistor inside the motion detector, which could just as easily be placed outside on a breadboard, a perf board....

When 3.3VDC was applied to the contact wires it measured 1.01mA when closed & 0 mA when open. 
From left to right I used the scheme of red for +12V, black for ground, green is one side of the relay contacts, and yellow connects to the other side of the contact (this has the resistor attached between the yellow wire and the screw terminal - I used a 'b connector' to connect it, which is common in security and telecom). It doesn't matter how you connect the green and yellow to your project board, as the circuit is either open or closed with a current limiting resistor in there - connect one two the Pi's ground, and one to your I/O pin which is configured as an input.

The only other thing I feel I should mention is that since we're using 22 gauge wire at 3.3V, the length of the wire should be fairly short, or local to the project board. If you wish to run it 20-50' away, I'd strongly recommend using the 12V line for the signal and using an opto-coupler or a transistor or something like that to achieve your low current low voltage signal to your controller.
Above is photo of it completed, reassembled, powered with contacts being monitored. Looks like everything is working great, and stylish too! Good luck with yours!

Monday, 20 October 2014

Hand Held A1302 Hall Effect Sensor (Gaussmeter)

This is a partial write up, I will expand on this soon. I wanted to put a schematic and code up for anyone who was interested in following up the YouTube video I posted about this!
Enjoy! PLEASE NOTE: I often add captions to correct myself or elaborate in these videos, but the text is usually only visible if you watch it on the YouTube website.

A lot of this project was inspired by this page: Cool Magnet Man This is an incredibly valuable resource if you are interested in projects involving magnets and magnetism - I strongly encourage you to read through his website, I've spent a fair amount of time enjoyably reviewing his posts.

I will apologize in advance, the code isn't amazing, the design is a little short changed - but this thing went together really quickly from concept to reality. I will likely have a software revision in the near future, if you find any bugs please let me know so I can improve the next version. This is designed to run from a 9V battery.


ARDUINO CODE:
(Some of the LCD positioning is funny because I was using an odd ball LCD for the breadboard)

#define sensorPin 0
#define calibrationPin 1

//#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

float sensorRead;
float calibrationRead;
float sensorPinVoltage;
float calibrationPinVoltage;
float gauss;

void setup() {
  lcd.begin(16, 2);
  lcd.print("Awesome");
  lcd.setCursor(1,1);
  lcd.print("Jarret!");
  delay (2000);
}

void loop() {

    sensorRead = analogRead(sensorPin);
    calibrationRead = analogRead(calibrationPin);
   
    if (sensorRead >= 1020 || sensorRead <= 3){
      lcd.clear();
      lcd.print ("INTENSIT"),
      lcd.setCursor(1,1);
      lcd.print("Y ERROR!");

    }
    else{
      sensorPinVoltage = ((5 * sensorRead) / 1023);
      calibrationPinVoltage = ((5 * calibrationRead) / 1023);
      gauss = ((1000 * (calibrationPinVoltage - sensorPinVoltage)) / 1.3);
      lcd.clear();
      lcd.print (gauss, DEC),
      lcd.setCursor(1,1);
      lcd.print("gauss");
    }
    delay(500);
  }


Sorry about being so blunt with this. I will elaborate more in the future.
Cheers, have fun!

Thursday, 28 August 2014

Playing with lab gear: FG-100 function generator and Leader 1020 Oscilloscope


This blog includes two videos: One where I play the FG-100 DDS Function Generator (EZM Electronics Studio) & a video where I focus on playing with the Leader 1020 20MHz Oscilloscope.

Video review and teardown of the FG-100 Function generator:
 

I found this function generator on eBay for only $32.59 USD from a Hong Kong seller, that's the final price! That price would not cover the shipping on a used lab function generator being shipped in North America. I won't repeat a lot of information that is covered in the video, but you should watch it to get a feel for how it works if you're interested in buying one. The specifications from the seller were as follows:

The main output waveform : sine wave and square-wave, triangle wave, sawtooth wave
Maximum output amplitude:± 10Vpp (no-load)
Output impedance :50Ω±10%
Dc bias: ± 10V (no-load)
Display: LCD1602
Output frequency range:
sine wave: 1Hz-500KHz
Square-wave: 1 Hz - 20kHz (the valid range)
Triangle wave :1 Hz - 20KHz (the valid range)
Sawtooth wave :1 Hz ~ 20kHz (the valid range)
Resolution :1 Hz
power supply: DC 3.5-10V

I would only put a 5VDC supply on this unit, I didn't measure the current draw, but I'd imagine it's fairly low, as you are only driving the generator, and not using it to power a load (which you shouldn't do!) - I'm not sure what the actual allowed current draw is on this thing either.
I did attach this thing to a newer digital oscilloscope with a frequency counter at one point, and the frequency was accurate to quite a few decimals, which makes me think the accuracy of the scope and the function generator were negligible at that point.
ALSO - No manual, you're on your own with this puppy, but you should be able to get it going on your own regardless.

Final Verdict: For less than $40 bucks you get something you can use to play around with, and would be fine for testing high and low pass filters, which makes it great for students or someone looking for a fairly accurate clock. If you're interested in playing with duty-cycle, it would seem you will have to do without that feature too.

The second video was me using the function generator to play around on my Leader 1020 Oscilloscope:
In this video I also play around with a capacitor and some of the features of the scope.
Manual can be found HERE.
I picked this up about 6 years ago, I think I paid a couple hundred for it, but it seems these are still being sold used for $100-200. I'm not actually sure how old this one is - It was last calibrated in 2008, and the serial number might hint that it's from 1980...
It has served me well, I wish the foot print was a little smaller, but I would rather this one than a cheap new digital oscilloscope. It's fairly easy to use, which makes it quick for most tasks. I personally find new digital scopes a little annoying at times with how many features they have and the endless possibilities for having something set up wrong (I find they also make young people lazy, they don't learn how to figure out frequency or voltages on their own).
But yeah, check out both the videos, hopefully you can decide if you want the gear I use, or perhaps you just wanted to see something or learn something.
Cheers!

Wednesday, 13 August 2014

Me, an Arduino and a DS3231 Real Time Clock


Check out the video above for a demonstration!

The grand project I am working on is a clock radio, and what good is it if it cannot accurately track time? It was tempting to try to set up the microcontroller to accurately track the time, but it would likely drift out fairly quickly. I decided that I would use an RTC (real time clock) module to keep track of the time for me. I looked around a little on google to see what others were using, and I looked around a little on eBay to see what was cheaply available and I settled on the DS3231. A few reasons for this:
  • Extremely accurate
  • Battery backup
  • I2C
  • Works with 5V
  • You can even get a temperature reading from it!
I purchased two units from eBay for $5.62US. Always buy at least two. If you wreck one, or one is broken then you don't need to wait - especially if ordering from China, you may need to wait 2-3 weeks to get your replacement!

They came in this nice static bag. On the bag it says "Raspberry Pi", which it seems like they were targeting this item to be sold to people who wanted to use it with the Pi, but it works just fine with any I2C capable device (I wonder if people looking for Arduino modules don't buy it because of the Raspberry Pi labeling?)





When you're looking around on the internet to buy these, it is important to remember that the DS3231 is the chip mounted on the board! They are not all created equal. This version only uses 4 pins - VCC, GND, SCL & SDA. It does deal with the battery on the board for you, which is nice, but you don't get a pin for reset, the 32KHz has an output on the chip as well as a pin for triggering an interrupt or sending out an adjustable square wave. If you want these features, you might want to see what other options are available for the DS3231.

It's sitting on a nice female header, which will make it easy to mount onto my perf board later. It came with that little battery - I doubt I'll ever replace it. It's everything I need, and more. I just wanted a 24 hour clock, this chip can track day, month, year and store 2 alarms.

Let's move onto the datasheet for this puppy, which can be found here: DS3231.pdf

It's only 20 pages, and a quick skim couldn't hurt. It picks up on page 11 when you get to see how the registers are formatted. It's all in BCD (binary coded decimal), so you need to work on the data before you send it out or display it - so '12' would be shown as '0001 0010' (I might do a video on bit manipulation, BCD and binary conversions some day). I'll attach some code to this blog entry, I feel it might be easier to see what's going on by looking at the code, rather than me try to explain it. The I2C is a little weird feeling on this, but I think I have a pretty good understanding after playing around with it for a little while.
I can try to save you some trouble with a mistake I made. They give an example of how to receive information from the data sheet - And no where in the data sheet do they tell you the address in plain English...
This might be my fault, but under slave address I was thinking a full byte had to be sent, but the 'slave address' was only 7 bits, so I put a '0' to the far right which I thought completed the 8 bits. So when I tried to address it, I was trying to talk to '0xD0', but for some reason you just read the 7 bits and imagine a zero as the MSB (most significant bit), which means the address was actually 0x68. Maybe if I spent more time playing with I2C devices this would have been more obvious? Who knows. I did learn something interesting from this: When I was trying to read data with the first address, I kept ending up with the retrieved bytes being 0xFF, which is all 1's in binary land, so for now on, if I'm working with I2C and I end up with nothing but 1's coming in the bytes, I'll assume either the address is wrong or the device is not connected - this could save troubleshooting time someday.
Moving along, I'm only really interested in the first 3 registers which handle the seconds, minutes and hours. I may yet do something with the temperature for fun.
You can look in the code to see the bit manipulation I used to extract the human readable numbers, it's quite easy once you get over the anxiety of dealing with thinking in binary.

One last hardware note: I'm holding up the lines to the 5V line with 4.7K resistors, with no issues.

Here's a picture of the Arduino board with the DS3231 module above. It ties on to the bus on the breadboard to the right, which also has the 4.7K resistors on it. The entire project is being powered by USB at this time.








This is the whole project with my radio portion (scroll down on the blog to learn more) TEA5767, the LM386, speaker, and of course, the DS3231.










For your viewing pleasure, another horrible paintbrush schematic! If you don't want the radio portion, you can ignore everything to the right of the DS3231 module.
The code includes the functions for the radio section. If you just wanted to use the getTime and initializeClock functions, you could cut and paste them - be sure to include the Wire.h header. Check back in the future to see this finished, the code will be more complete and I will have hopefully settled on how to have the user interact with it all.

#include <Wire.h>

unsigned char frequencyH;
unsigned char frequencyL;

unsigned int frequencyB;
double frequency;
double tunerFrequency;

unsigned int mainLoopCounter;

void setup()
{
  Wire.begin();
  frequency = 95.9; //A fancier approach would be to store this in EEPROM and retreive last channel ;)
  Serial.begin(9600);
  setFrequency();
  initializeClock(); //set the time in the initialize clock function and uncomment this line, upload,
                     //then comment this line out. It will retain your original time and not overwrite.
                     //A proper setting method will be incorporated later!
 
  Wire.beginTransmission (0x68); //if the clock isn't on register 0x00 at startup, it won't work properly
  Wire.write(0x00);
  Wire.endTransmission();
}

void loop()
{
  int reading = analogRead(0);
  //Serial.println(reading); //Shows the adc position - setting it to 512 will point "up" so you can attach a knob
  if (reading <= 410 || reading >= 614) checkTuner(); //only check tuning if tuning knob leaves rest area
 
  mainLoopCounter ++; //I know this is a little lame, won't be part of final
  if (mainLoopCounter == 2000)
  {
    getTime();
    Serial.println(frequency);
    mainLoopCounter = 0;
  }
}

void checkTuner()
{
  int currentReading = analogRead(0);
  if (currentReading <= 205) frequency = frequency--; //Tune back by 1MHz
  else if (currentReading >= 206 && currentReading <= 410) frequency = frequency - 0.1; //Tune back decimal
  else if (currentReading >= 614 && currentReading <= 819) frequency = frequency + 0.1; //Tune forward decimal
  else if (currentReading >= 820) frequency = frequency ++; //Tune forward by 1MHz
  else frequency = frequency; //If something unexpected happens, this will buffer it out
  if (frequency < 88.0) frequency = 88.0; //make sure it doesn't tune too low *Band limits can be adjusted
  if (frequency > 108.0) frequency = 108.0; //make sure it doesn't tune too high
  delay(300);// this delay will only occur if the main loop makes it into this function
  Serial.println(frequency);//only prints frequency while being tuned
  setFrequency();
}

void setFrequency()
{
  tunerFrequency = ((int)(frequency * 10)) / 10.0; //fun use of a cast!
  frequencyB = 4 * (tunerFrequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0x1A);//0001 1010 -highside injection, forced mono, "right channel" muted, *PINOUT AND 3RD BYTE HAVE CHANNELS SWAPPED
  Wire.write(0x10);//Set for US and Europe band
  Wire.write(0x00);
  Wire.endTransmission();
}

void getTime()
{
  Wire.requestFrom(0x68,19,true); //its polling all 19 registers so it will be back to register 0x00 on next read.
  byte second = (Wire.read());
  byte minute = (Wire.read());
  byte hour = (Wire.read());
 
  //converting from BCD to decimal
  Serial.print(hour >> 4 & 0x03);
  Serial.print(hour & 0x0F);
  Serial.print(":");
  Serial.print(minute >> 4);
  Serial.print(minute & 0x0F);
  Serial.print(":");
  Serial.print(second >> 4);
  Serial.println(second & 0x0F);
}

void initializeClock()
{
   Wire.beginTransmission(0x68);
   Wire.write(0x00); //start at address 0x00
   Wire.write(0x00); //seconds - enter numbers how you would normally see them
   Wire.write(0x44); //minutes - these numbers would set the time to 22:44:00
   Wire.write(0x22); //hours (24h)
  //NOTICE: I only had to specify register once, it can increment the register on its own.
   Wire.endTransmission(); 
}


That's all I have to say about this for right now. Stay tuned to see what happens next. Hopefully this will help someone else working on something similar.

Good luck with your project!

Wednesday, 30 July 2014

TEA5767 FM Radio Receiver with Arduino and LM386 Amplifier Part 2


I spent the week playing around with how the radio was working, I wasn't horribly impressed with how it was working after my last post. I think I've pretty much nailed it for what I want to do, I even fixed a few kinks. I do strongly recommend you check out the post behind this one to see links to where software and design material came from. I also mentioned that the way I mounted the TEA5767 was similar to what another individual did, and here is a link to his blog: http://www.doctormonk.com/2012/03/tea5767-fm-radio-breakout-board-for.html

I've also noticed that this posting has been receiving a lot of attention, and I decided it may not provide enough information for someone trying to replicate it, so I made a quick and dirty paintbrush schematic:
For both pots I used a single turn 10K potentiometer.  You should also tie up the SDA and SCL lines up to 5V with a 4.7K resistor - this module seems to work just fine without it.
I understand the schematic isn't great and should be a CAD drawing, but it's simple enough. I'd urge you to dig up the data sheet for the LM386 to look at the different configurations. Hopefully this schematic will make this post more useful! This is a component of a larger project I'm working on, so hopefully at some point I will have it drawn properly in its entirety.

Because of how mine is done, this will be a daughter board soldered onto a main perf. board.

I'll make a programming note here:
You can see in the picture that I soldered onto the middle pin on the right side of the chip, which is suppose to be LEFT CHANNEL. If you decide you want to mute the RIGHT CHANNEL by adjusting the 3rd byte, they are backwards - you will end up muting the wrong one!! I'm not sure if  the issue is that you're turning off the wrong channel, or if the pins are flipped - if you're running stereo, this could mean you have LEFT AND RIGHT backwards. If you're running mono, like I am here, it won't matter - but I feel this is a fairly large ooops in a data sheet.

One thing I really didn't like about how my first attempt worked was that tuning with a 270 degree potentiometer was horrible. It just doesn't have enough resolution - if you actually tuned the channel you wanted, it would weave in and out of it, making it pretty intolerable. The solution I came up with (and this is really because I wanted to keep the number of knobs and buttons to a minimum) was to use the ADC to use different areas of the knob to achieve different results.

To the left you can see the scheme I used to control the tuning. It was a little weird at first, but I seem to have gotten the hang of it. If the knob is pointed up, nothing happens. Turn it a little to the left and it decreases by 100KHz, Turn it further, it decreases by 1MHz. It's buffered by a 300ms delay, which you could tweak to your tastes. I'm not in love with it, but it seems to work well for using the materials I have and getting it to tune in a satisfactory way. I will likely use the same method for setting the time on the clock portion of this project.

Here is what some of  you may have been skimming for, the code:

#include <Wire.h>

unsigned char frequencyH;
unsigned char frequencyL;

unsigned int frequencyB;
double frequency;
double tunerFrequency;

void setup()
{
  Wire.begin();
  frequency = 95.9; //A fancier approach would be to store this in EEPROM and retrieve last channel ;)
  Serial.begin(9600);
  setFrequency();
}

void loop()
{
  int reading = analogRead(0);
  //Serial.println(reading); //Shows the adc position - setting it to 512 will point "up" so you can attach a knob
  if (reading <= 410 || reading >= 614) checkTuner(); //only check tuning if tuning knob leaves rest area
}

void checkTuner()
{
  int currentReading = analogRead(0);
  if (currentReading <= 205) frequency = frequency--; //Tune back by 1MHz
  else if (currentReading >= 206 && currentReading <= 410) frequency = frequency - 0.1; //Tune back decimal
  else if (currentReading >= 614 && currentReading <= 819) frequency = frequency + 0.1; //Tune forward decimal
  else if (currentReading >= 820) frequency = frequency ++; //Tune forward by 1MHz
  else frequency = frequency; //If something unexpected happens, this will buffer it out
  if (frequency < 88.0) frequency = 88.0; //make sure it doesn't tune too low *Band limits can be adjusted
  if (frequency > 108.0) frequency = 108.0; //make sure it doesn't tune too high
  delay(300);// this delay will only occur if the main loop makes it into this function
  Serial.println(frequency);//only prints frequency while being tuned
  setFrequency();
}

void setFrequency()
{
  tunerFrequency = ((int)(frequency * 10)) / 10.0; //fun use of a cast!
  frequencyB = 4 * (tunerFrequency * 1000000 + 225000) / 32768;
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB;
  Wire.beginTransmission(0x60);
  Wire.write(frequencyH);
  Wire.write(frequencyL);
  Wire.write(0x1A);//0001 1010 -highside injection, forced mono, "right channel" muted, *PINOUT AND 3RD BYTE HAVE CHANNELS SWAPPED
  Wire.write(0x10);//Set for US and Europe band
  Wire.write(0x00);
  Wire.endTransmission();
}


I won't take credit for this in its entirety, between the link I posted above and a blog mentioned in the previous post, is where some of the work came from. You will want to open the serial monitor window in your Arduino IDE to see the frequency. I implemented my tuning method in the checkTuner function. I also tweeked out the setFrequency function, it seems to have fixed an issue I was having with the tuning drifting by 0.1MHz (I don't know why). I also changed that it only sends a message out to the I2C bus only after a change is requested by the checkTuner function - I was getting a ticking sound from it constantly communicating ~200ms. So I have it tuning better and less clicking. I also adjusted some of the data bytes to better reflect my needs. I found a detailed data sheet here:
http://www.voti.nl/docs/TEA5767.pdf 

The one last issue I had was trying to get it to scan for channels. I never got it to work properly, if you have it sorted out, let me know! 

So yeah, check out the video for the demonstration, and stay tuned to see where this project ends up. I hope this helped if you were looking for guidance!

If you want to see this project with a real time clockadded to it, check out a more recent blog which uses the DS3231 RTC combined with the project you just read about here: Arduino + DS3231 Real Time Clock