Sunday, January 8, 2012

SensorBox 1 source code

The SensorBox found upstairs for the 1500L water tank.



SensorBox 2 source code

The source code for the 300L tank found downstairs under the surface of the ground.


DisplayBox Secondary source code

This DisplayBox just receives the data and displays it on the 4 x 20 character LCD.  Physically located in my  mother's kitchen over the washing machine.





DisplayBox Main source code

Its main role apart from displaying the data on LCD is to act as Master for the communications.  The frame that is sent is also formatted here with the array _buffer[].




Saturday, January 7, 2012

Code used to fine tune the Analog circuit

Code I use to calibrate the ultrasonic sensor and debug the analog part of the circuit which is made of 3 Op Amps in Non-Inverting Amplifier configuration with an Op Amp as voltage follower in its first stage.  The PIC outputs the data through its USART which is connected to the development board to the RC6 pin.  The development board is in turn connected to the serial port/RS-232 of the computer where which it is logged using the USART Terminal of the mikro C PRO compiler and saved as a text file.  Any other communication program understanding the serial port would do the job.






Below is the sample output of the above program graphed using Excel spreadsheet.



PCB Layouts

Initially I wamted to use one common design/layout for the SensorBox and DisplayBox.  The PCB was designed using ExpressPCB which is free.  The PCB would have the headers to connect the 4x20 character LCD display as well as host the MCP6284 quad Op-Amps for the analog receiving part of the circuit.  The analog part was copied from the mikroElektronika Accessory Board named DistanceMeter.  Its schematic is found below.  Unfortunately I can't find it anymore on their website.  Their Accessory Board was using a Maxbotix MB1100 transducer.



I also went to the Maxbotix web site where I found that their products MB1000 and MB1040 were using the same analog design for processing the received signal with 3 Op Amps in the IC LM324 as shown below.  Pay attention to the similarity in the 2nd half of the circuit, below the PIC16F676.  Maxbotix gives a lot of information in the circuit operation follow the link.



The first PCB which I then designed is shown below.  If you want the native ExpressPCB format email me.


After this design I made a second PCB design shown below, just for the SensorBox.  I had to remove the display from this one as I was going to use the RB0 ~ RB3 ports (pins 21 ~ 24 of the PIC16F886)  for variable gain.  I got the idea for this variable gain receiver or Adaptive Amplifier with increasing Amplification during Echo waiting Time from the following website.  The schematic of the analog part is shown after.  In this design the three Op Amps are in Non-Inverting Amplifier setup.  Its the third Op-Amp which has a variable gain over the time domain for sampling.


The 2nd PCB Analog Schematic is shown below.  Basically its three Op-Amps in Non-Inverting setup in a row with an envelope detector in the end.

The 3rd PCB layout is shown below, hope it solves my problems.  I have added 2 bypass capacitors C4, one near each IC on the circuit whose value would be 0.1 µF/100nF plus 2 other bulk capacitors C21 & C22. C21 vlaue would be 4.7 µF while C22 would be around 1 ~ 2  µF.

How I re-Invented the wheel with RS485

For the communication part between the PICs, I unknowingly re-invented the wheel with RS-485, which I realised after reading the application AN774 page 8 fig7 "TYPICAL RS-485 FOUR-WIRE CONNECTION" from Microchip in May 2011.  Between November 2010 and May 2011,  in my terms/flavour for the communication setup I would have one device which would be the master and the remaining devices as slaves.  The master in a round robin would query each slave one by one.  The slave would only send back its data if the master was addressing it.  That meant each device would have a unique ID.  In my implementation the master or slaves would send frames of 32 bytes or 256bits length.  Each frame would start with the string ABCXYZ and end with the string ZYXCBA, the remaining 20 bytes in between would contain destination field by device ID, originating device ID field and the rest would be dedicated to 5 data fields.  The ABCXYZ and ZYXCBA strings is placed to ensure that the PIC do not mix the data frames.  The communication module uses the TIMER0 interrupt to prevent the PIC from hanging and detecting timeouts for data transmissions.

The code which I wrote before coming across the RS485 is the same that is working today, I just had to add 4 lines to enable the MAX485 OE pin during receiving mode for the master.