Maker Pro
Maker Pro

Designing a Time Delayed Relay

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Trevor, my Fairchild 2N3904 datasheet shows EBC for the TO-92 package only. The only other pin-outs shown apply to their SMD packages.
When I'm declared King of the world EBC pin-out will be one of my first decrees. :D

Chris

Thanks Chris, I think I will follow the simple route and unsolder the existing part and fit in a new one with the legs bent to make it to the crossed holes. A little bending with pliers and perhaps some heat shrink tube for added protection and we're off again ;)
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Believe it or not, it took three hours to desolder three legs.... I had a solder sucker, solder wick, flux gel and had to use them all as well as some other tricks like retinning the area, using another lead to super heat and push through, etc. I give much credit to the board manufacturer, the board held up like a pro. No damage, no lifting of the trace, etc. Tomorrow I will bend the leads and fit in the replacement and do another test power up.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Trevor, I was going to start a new topic ("Desoldering The Easy Way") in the Resources section but it says I have insufficient privileges to post there?o_O No biggy though because It really belongs in a Tips & Tricks section that we don't have.

Anyway here it is, less some photos I was going to take.....

The Soldering Gun is a tool that we don't normally regard or accept as a proper soldering tool but it can be your friend when it comes to desoldering multiple pins. Using 12 gauge solid copper wire you can form just about any heater loop shape imaginable, even round!

Just last week I had to desolder a miniature transformer (6 pins) from a double sided board. I simply bent the wire in the form of a rectangle with one side open. The open side was the soldering gun loop out - in. I then taped a string to the xfmr and tied a small weight to the opposite end. With the foil side of the board facing up I heated all six joints 'simultaneously' and the little weight did the rest.

I've done this many times with great success. When I do I always prep the joints & wire loop with additional solder to insure maximum heat and solder contact. That said sometimes you have to rock the gun if the loop is not making good contact with all the pins.

After the component is removed it's much easier to solder suck an empty pad. That said I usually just heat each pad with an iron and give the board a light rap on the bench. This clears solder out of empty pads easily.

Chris
 
Last edited:

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
@CDRIVE - that is brilliant Chris, well done. I wish I had thought of that!! I will have to pickup a soldering gun on the cheap to make into your desoldering idea ;)

Success, board saved, new transistor fitted into place. I did a little foldma-gami on the pins :rolleyes: and it popped right in. Tested and seems to be working.

Hop had mentioned to prototype by attaching small wires to the allegro chips, but as I looked at the board without the pic installed I realized that I can access the output of the Allegro from the pic socket! Pins 3 and 5 are the Allegro chips outputs so I can use this to mockup a current draw to give me an alternating AC to test out the software!!

transistor_zpsecm40xwb.jpg
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
This is not a recommendation nor a endorsement. That said Horror Freight sells em cheap! You can also get a Weller brand at reasonable prices on Fleabay. On that note a high wattage Soldering Gun is useful for just about anything that requires muchas heatas. It was always my choice of tools when I was soldering copper braided cables like RG58, RG59 and especially RG8 RF coax. Even more so when soldering it to a large ground plane.

Chris
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Back to software..

I have a test rig set up now to make life easier :D

20160822_165924_zpsidfn9xrv.jpg


yes, very ghetto, but it works and keeps things safe.

@hevans1944 - check it out Hop :D
:cool::D:cool::D
20160822_183913_zpsckwu2iuv.jpg


I used the wife's hair dryer plugged into that outlet in the test rig to give me a 4A draw through the run delay board circuitry, I used a jumper from the empty pic socket pin 6 to be my output to the scope. Nice readout, eh? My DMM gave me 0.363VAC so I guess that is close enough.

The next thing I tried was putting that jumper over to my pic dev board 2.0 on pin 6 and removing the on board jumper to disable the pot that was on that line. I fired up the run delay board with latest code (see below) and got..... nada - the light that was supposed to turn on when current was running was turned on as soon as the programming was done! I thought three things - a timing issue in the code, I tell it to turn on in the code, but the code is wrapped in a forever loop - while (1)? The port needs to be told to start in the off position? There is a flaw in the logic leading to a true condition outside of the parameters specified (well duh, right!) <sigh>

Does anything strike anyone in the code as to why I should be getting a light on condition on GP5 before even hooking up anything to AN1?

Thanks in advance!


Code:
// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // GP3/MCLR pin function select (GP3/MCLR pin function is MCLR)
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF         // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>


#define _XTAL_FREQ 4000000                      // Setting internal clock to 4MHz

int main(void)

{
  // SET UP
  int AN3_AD_IN_110 = 0;                        // variable to hold value of A/D input from U2 Allegro chip, 110v line.
  int AN2_AD_IN_220 = 0;                        // " from U3 Allegro chip, 220v line.
                                                // This signal is baseline of 2.5VDC with a 0-2.5VAC signal riding on top.  
  int max = 511, min = 511,    
          max1 = 511, min1 = 511;               // variables for min/max comparisons on both channels 110/220 
  int temp_value = 0, temp_value1= 0, 
          peak = 0, temp_peak = 0;              // Hold temp values to compare ADC against
  int count = 0;
  ADCON0 = 0b0000101;       // Starts converter with first bit, third bit is AN1 //0b0001101;    // AN2 is analog, turning on analog channel and starting converter
  TRISIO = 0b000010;        // AN1 is input //0b001100;     // Only AN2 and AN3/MCLR are inputs all else outputs.


 while (1)
 {          
    ADCON0 = 0b0000101;     // AN1 is analog input     //0b0001101;                         // AN3 is analog, turning on analog channel and starting converter
    //ANSEL =  0b0110010;          //0b0111100;                         // AN2 and AN3 set to Analog, other 2 pins are digital, 011 on left side indicates FRC for timing
    ADCON0bits.GO_DONE = 1;                     // starts A/D conversion cycle        
    AN3_AD_IN_110 = ADRESL + (ADRESH << 8);     // 1024 bit resolution 5v/1023 = 4.88mV per bit
      if (AN3_AD_IN_110 >= max)                 // check for 511 (1/2 Vcc) or greater
        { 
          max = AN3_AD_IN_110;                  // set variable max to 511 or greater
        }
      if (AN3_AD_IN_110 <= min)                 // check variable min for 511 or less
        { 
          min = AN3_AD_IN_110;                  // set var. min to 511 or less
        }
   
     temp_peak = max - min;                     // temporary peak value established and used to compare later  
   
     if (count < 128)                           // set counter to prevent infinite loop
        {  
            count = count + 1;                  // incrementing counter
        } 
      if ((temp_peak >= 144) && (count == 128)  // control statement - if peak value is above threshold and we iterated 129 times
           && (max >= 584) && (min <= 438))     // and if 0.354V above and below Vcc - (110v threshold) then proceed
        { 
          _delay(2);                            // 2 second delay before turning on
          //GPIO = 0b000001;                    // set GP0 high to turn on relay
          GPIO = 0b100000;  //REMOVE ONLY FOR TESTING // this is to check code on dev. board - GP5 outermost LED to corner should light
          //ADCON0bits.GO_DONE = 1;             // do A/D measurement
          count = 0;                            // reset count variable to zero for next go around
        }  
               
         
           

    if ((temp_peak <= 130) && (max <= 560) && (min >= 490)) 
            {
               _delay(5);                       // statement checks to see if current has dropped below threshold
               GPIO = 0b000000;                 //waits 5 seconds then shuts off GP5
            }  
}
}
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Nah, that's not ghetto. That's a typical and functional test fixture.;)

Chris
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
LoL, thanks Chris.

I reviewed my code last night and intend to add some lines to enable the GP4 led on the pic dev board to light and clear each time my counter hits 128 to try and see why GP5 comes on and stays lit and to see if I am cycling through the program (and perhaps how fast that cycling is taking place!). My thoughts are that my if statements are not functioning as intended.... I debugged through Mplabs and stepped through each line as it actuated, fed in variables to min and max and the flow between if statements was correct, but something is just not working in real time. Perhaps its a settings issue with one of the registers... I will have to review that later.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
That would be Hop's department as I don't speak ASM or C of any flavor. I did try playing with ASM once but when I asked myself if I was having fun yet a little voice responded with another question... "Chris, do you enjoy root canals"?

Chris
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
That would be Hop's department as I don't speak ASM or C of any flavor. I did try playing with ASM once but when I asked myself if I was having fun yet a little voice responded with another question... "Chris, do you enjoy root canals"?

Chris
:DLOL:D

Chris, this may be true, but I ask myself this.... "Do you enjoy eating?" and since the answer is yes, I persevere. Coding may not always be fun, but its necessary to get these little devices to do what we want ;)
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
I don't mind coding but Basic is the only language I'm qualified to handle.

Chris
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
I don't mind coding but Basic is the only language I'm qualified to handle.

Chris
Years ago a "professional" told me that BASIC was not a good programming language and I was wasting my time with it. This was the first computer language I learned and I loved it! So easy to write code and get immediate gratification that I had done it right. The so-called pro sniggered at my attempts at coding, claiming that BASIC had no structure and encouraged the writing of "spaghetti code" sprinkled liberally with GOTO instructions.

Apparently "real programmers" have no need for a GOTO instruction. I ignored him and several years later, when I could afford it, I bought a PC with BASIC "built in" and eventually "upgraded" to Microsoft FORTRAN and Microsoft Visual BASIC compilers. Microsoft VB was the first language that I encountered (outside of MACRO assembly) that worked with the Windows OS to implement event-driven code. Later I also purchased Microsoft Visual C, but never felt as comfortable with using that.

The early version of Microsoft FORTRAN was a company purchase for a job I was working on at the time. It was the only version available at the time that would compile FORTRAN-77 code that was 100% compatible with DEC PDP-11 minicomputers running RSX-11M OS. I had at my disposal one of those computer pros whose job it was to write the FORTRAN program to run on the IBM-compatible PC (actually a REAL IBM PC-AT) I was embedding in a scanning micro-densitometer used to digitize classified aerial reconnaissance film (second generation high-resolution copies of the originals). The company I worked with was a DEC shop and I got a lot of resistance for choosing the PC platform instead of the PDP-11 minicomputer for the embedded controller.

So this "pro" wrote all the code at his time-sharing video terminal, deigning to soil his hands on a PC. We ported the source to the IBM PC for compilation there, after I got my custom hardware interface to a Hewlett-Packard plane-mirror laser interferometer up and working. This company I worked for later folded and DEC went out of business, but AFAIK the Mann-Data Micro Densitometer our three-man team put together still works. Of course now all overhead reconnaissance is being done with digital imagery instead of film, so it's probably been sold for scrap by now.

If I had had more cojones at the time, I would have required Mr. FORTRAN Pro to write the code in Visual BASIC, but this was a DEC shop and I barely was able to convince management that an IBM PC-AT was adequate for the job. Trying to push Visual BASIC onto the software "pro" would probably have cost me my job. This guy did everything he could to break the Microsoft FORTRAN compiler, but it performed just like the DEC compiler and he eventually gave up trying to break it and wrote some pretty decent code. Of course I had to write the hardware interface code in Microsoft MACRO assembly, but that was pretty simple IMHO.

So, yeah, Chris... BASIC is fine! If you haven't already, try to score a copy of VB 6 before it disappears from Planet Earth.

Hop
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
Hop, I've had a registered copy of VB6 Pro since 1998. I've written reams of code in VB6 and the only.. and I do mean only "GoTo" code I've ever written is in conjunction with "On Error GoTo". So Yes, even in VB6 GoTo is something to be avoided and considered spaghetti code. Picaxe Basic is a different animal. It's hard to avoid GoTo but it does provide GoSub.

BTW, the crash of Planet Source Code was a travesty but VBForums is still healthy!

Chris
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,878
Joined
Jun 21, 2012
Messages
4,878
BTW, the crash of Planet Source Code was a travesty but VBForums is still healthy!
Believe-it-or-not, I had never heard of Planet-Source-Code.com until your post! Thank your for that. So, I visited their website, joined up, and now am ready to upload a killer app for all the world to see... as soon as I find my "round tuit" and write it.:cool:

Geez, this thing has been around for a long time (15 years?) so the system crash (caused by "technician error" in June 2015) could have been a deal buster. But it looks like someone was able to "reverse engineer" the database and restore much of the source code that was lost. Hopefully this won't happen ever again now that they are using the Microsoft Cloud server. I still plan to order the CD-ROM though... just in case.

Looking at programming (coding) from a non-software point of view has not been a big disadvantage for me in embedded systems design. When I was first exposed to digital computers in the 1960s, most of them were so-called mainframes or "big iron" with a few so-called "minicomputers" sprinkled here and there for "scientific" uses. At that time computer science students were directed to learn abstract concepts appropriate to massive background processing tasks typical of business applications with massive databases. I read a lot of "computer science" books aimed at teaching structured programming principles, but none of it seemed appropriate to "real time" computer control and none addressed hardware I/O. So I was on my own until the Internet came along and opened up an infinite supply of learning resources. I didn't have enough time to spend in the library or enough money to purchase "professional" programming texts, although I did buy a few dozen that are now hopelessly dated.

I doubt I will ever be able to call myself a "professional programmer" even if I do get paid for writing code. I hate re-inventing the wheel but fail to adequately document and preserve my code for re-use. Bad habit from always doing "one off" projects. When project is done, documentation ceases. So, if not budgeted up front, no documentation delivered with the project except for source code, schematics (maybe) and hand-written notes to myself on how I got the hardware interfaces to work.

But keep on truckin' Chris... I never really got the "hang" of cut-and-paste VB, but did do a few projects that way. Lots of fun. And the concept has caught on with system integrators, as witness the huge number of Lab View programs out there now. BTW, I HATE programming in Lab View. Linux on steroids IMHO.

Hop
.
 

CDRIVE

Hauling 10' pipe on a Trek Shift3
May 8, 2012
4,960
Joined
May 8, 2012
Messages
4,960
I've never used PSC or VB Forums for cut and paste (as is) but both sites have proven invaluable for either building an apps foundation or patching holes in my own. If it were not for a PC's Serial Port I doubt that I ever would have ventured into programming. Fact is I'll always be a VB6 Duffer with the exception of VB's MSComm Control. When RS232 began disappearing from PC's in favor of USB I was having fits until I discovered that USB/Serial converters worked seamlessly with VB's MSComm.

Chris
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Thanks again @NorthGuy for clarifying the logic of the code!

So at this point we have one power input read into the ADC and the circuit working successfully! Hurray, 2 years 6 months later :oops::D

The next step is to modulate the second ADC input from the other allegro chip. There are two ADC channels being used on the pic, but only one can be turned on at a time. As I have written the code, I would have to iterate through 128 times to poll my min/max values for the first channel, exit that while loop, establish my temp_peak and then reset my min/max to Vcc before I could insert another call to start the other ADC channel. I could then OR my if(temp_peak) statements to include the second ADC inputs, such that if either sensor has current running through them, the relay goes high.

I am thinking of adding the code right above the line incrementing the count variable. Any suggestions on how to write this better? I do realize that this code is very juvenile at best, but I am only moderately familiar with C constructs and have not used the pic timers yet. All comments welcomed, thanks!

Code:
int main(void)

{
  // Variable Declaration
  int AN3_AD_IN_110 = 0;                        // variable to hold value of A/D input from U2 Allegro chip, 110v line.
  int AN2_AD_IN_220 = 0;                        // " from U3 Allegro chip, 220v line.
                                                // This signal is baseline of 2.5VDC with a 0-2.5VAC signal riding on top.  
  int max = 511, min = 511,    
          max1 = 511, min1 = 511;               // variables for min/max comparisons on both channels 110/220 
  int temp_value = 0, temp_value1= 0, 
          peak = 0, temp_peak = 0;              // Hold temp values to compare ADC against
  int count = 0;
   
  //ADC Setup
  ADCON0bits.ADFM = 1;                          // Right justified A/D results
  ADCON0bits.VCFG = 0;                          // Voltage reference set to Vdd
  ADCON0bits.CHS0 = 1;                          // Channel select 0 register set to 1 to select AN1
  ADCON0bits.ADON = 1;                          // Turn A/D converter on
  __delay_us(15);
  TRISIO = 0b000010;                            // AN1 is input :old comments //0b001100;     // Only AN2 and AN3/MCLR are inputs all else outputs.
  GPIO = 0b000000;                              // Turn off all outputs initially
  //Analog Select Register Setup
  ANSELbits.ADCS = 0b101;                       // Clock derived from internal osc - max of 500kHz - problem with 4MHz xtal freg??
  ANSELbits.ANS1 = 1;                           // Sets AN1 as analog input (page 46 of pic675 manual states this and trisio need to be set)
       
 while(1) 
 {  
    while (count < 128)                              // set counter to prevent infinite loop
       
     {
         // Start A/D conversion process
        ADCON0bits.GO = 1;  
        __delay_us(15); 
        while (ADCON0bits.nDONE){}                  // Wait for conversion to be done
         //
        AN3_AD_IN_110 = ADRESL + (ADRESH << 8);     // 1024 bit resolution 5v/1023 = 4.88mV per bit
   
          if (AN3_AD_IN_110 >= max)                 // check for 511 (1/2 Vcc) or greater
            { 
              max = AN3_AD_IN_110;                  // set variable max to 511 or greater
            }
   
          if (AN3_AD_IN_110 <= min)                 // check variable min for 511 or less
            { 
              min = AN3_AD_IN_110;                  // set var. min to 511 or less
            }
        count = count + 1;                          // incrementing counter 
        if (count == 128)
        {
         GPIO4 = 1;                        //    write a =! GPIO statement to turn led GP4 on and off every 128 cycles
         __delay_ms(50);
         GPIO4 = 0;
        }
     } 
         
     temp_peak = max - min;                     // temporary peak value established and used to compare later  
     min = 511;                                 // reset variable 
     max = 511;                                 // reset variable 
     
     if (temp_peak >= 80)                       // control statement - if peak value is above threshold 
                                                // and if 0.354V above and below Vcc - (110v threshold) then proceed
        { 
          __delay_ms(2000);                     // 2 second delay before turning on
          //GPIO = 0b000001;                    // set GP0 high to turn on relay
          GPIO5 = 1;  //REMOVE ONLY FOR TESTING // this is to check code on dev. board - GP5 outermost LED to corner should light
          //ADCON0bits.GO_DONE = 1;             // do A/D measurement            
        }  
           
      if (temp_peak <= 60)  
        {
          __delay_ms(3000);
          GPIO5 = 0;
        }
           
      count = 0;                           // reset count variable to zero for next go around
    }
}
 
Top