Connect with us

Picaxe programming help

Discussion in 'Microcontrollers, Programming and IoT' started by irishluck, Nov 25, 2015.

Scroll to continue with content
  1. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    So im having a little trouble understanding variables with Picaxe. I have read the manuals and watch some videos and just still confusing to be honest.

    So im using the picaxe 08 chip. Basically all im trying to do is use a photo cell that will have a constant laser aimed on it. When the beam is broken I want it to trip a relay on for a certain amount of time
    Now Ive been told by a member here on the forum that a photocell will have to use the readadc command, which I am but Im just not completely understanding how to use variables.

    So can anyone help me out here?

    So here is the code I have so far.
    Code:
    main:
        readadc c.1,b0
        if b0 > 50 then high 4 goto ldr1 endif
        if b0 < 50 then low 4 goto ldr2 endif
        goto main
       
    ldr1:
        high c.4
        wait 5
        goto main
       
    ldr2:
        goto main
    And here is a video of what its doing so far,



    Here are a few pictures too.

    [​IMG]

    [​IMG]
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,069
    2,150
    Nov 17, 2011
    Certainly, if you state your question clearly. What is it that doesn't work as expected and what do you expect? What happens instead?

    Basically, instead of creating spaghetti code by jumping around your code with GOTO commands, use the built-in commands for structured program flow control. Without going into the details, your code can be better structured, therefore better readable (and understandable) and in consequence better maintainable using a structure similar to this framework:
    Code:
    main:
    
    do
       readadc c.1,b0
       if b0 > 50 then
          do_some_stuff
          do_some_more stuff
       else
          do_whatever_required_for_b<=50
          do_maybe_something_more
       endif
    loop
    This code will continually loop between "do" and "loop",read the ADC into b0 (readadc) and check the value of b0 (if). N ote that instead of comparing b0 twice (as you do for <50 and >50, what about =50?), this code checks only for b0>50 (then branch) and if this is not the case, b0 is automatically <=50 and the code in the else branch is executed.
     
  3. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Ah yea sorry.
    Basically I cant get the phtotocell to work at all.

    In the video I made you can see me covering the photocell and the LED goes out but at the end of the video you will
    see my DMM and it doesn't go to 0v at any time.

    Thats what I dont understand though. I dont get the whole b0,b1,b2, etc and the variables you set for it.
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    10,069
    2,150
    Nov 17, 2011
    That's not an issue of variables but of your hardware setup. The photocell is a light-sensitive resistor. If will change the resistance in the presence or absence of light (no light, high resistance and vice versa). In order to evaluate the change in resistance, you need a kind of reference. A very simple setup is by using the photocell as one leg of a resistive divider where the second resistor serves as reference. The voltae at the tap of the divider will be low or high depending on the amount of light falling onto the photocell. But it will never be 0V, as the divider can divide the voltage across it to small values, but not zero.

    A schematic of your circuit would be helpful to analyze an possible issue.
     
  5. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    I guess i didnt mean 0v exactly. Cause i think with the light and no light the difference in voltage is its slightly over 5v and then when its dark i do believe 4.65v.
    Ill have to recheck that.
    Im wanting to have the photocell turn on an output pin on the picaxe that will trip the relay.


    I can draw a new schemetic tonight after work and school. I have one already drawn up but without the LED's in it. Those are just for me to see whats turning on and whats not turning on.
     
  6. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Here is a schematic for ya.

    The LED's are not in there and also the caps that are before and after the voltage regulator.
    But just so you know, there are 390ohm resistors in series with the LED's. On the voltage regulator there are 10uf caps on the input and output. There will be a 1uf on the input when it it arrives.

    [​IMG]
     
  7. Harald Kapp

    Harald Kapp Moderator Moderator

    10,069
    2,150
    Nov 17, 2011
    I was afraid your circuit might look somewhat like it does.

    Your LDR is in series from Vcc (+5V) to teh input of the PicAxe. Assuming an input resistance in the >1MΩ range, any change in teh LDR's resistance will be negligible compared to the input resistance. Therefore only a very small current can flow and the main voltage drop occurs across the input resistance of the PicAxe. This leads to what you experience: only a very small change in voltage on the PicAxe's input pin when the LDR changes from light to dark.

    You need to add a resistor from teh PicAxe's input to ground with a value less than the dark value of the LDR. Read this information to bring you up to date on LDRs.
     
  8. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Ah yes but I did end up putting in a resistor last weekend. Sorry, forgot to mention that one too.
    It has a 10k resistor in there.

    I read that on the picaxe manuals that I needed one there.
     
  9. Harald Kapp

    Harald Kapp Moderator Moderator

    10,069
    2,150
    Nov 17, 2011
    The resistor is not in your schematic. How is it connected?
    The value will also depend on the properrties of teh LDR. What is the resistance of the LDR in light and when dark (you can measure that with an Ohmmeter or multimeter in Ohm range))
     
  10. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Hopefully this will be a better schematic.
    I forgot to draw this in.

    I will measure the resistance of the LDR and get back to you.

    [​IMG]
     
  11. Harald Kapp

    Harald Kapp Moderator Moderator

    10,069
    2,150
    Nov 17, 2011
    Much better. And the resistor is in the right place, too.
    Depending on the type of LDR, you should expect a value in the 10 kΩ range in the dark and <=1 kΩ when lighted. If this is the case, 10k for the resistor from pin 6 to ground is a reasonable value.

    The voltage on pin 6 should vary between ~5 V and ~2.5 V. If you want it to go to less than 2.5V, make the resistor smaller. The resistor and the LDR form a resistive voltage divider which you can adapt (within limits) to different voltage ranges at the output.

    There is one issue that might be resonsible for you not seeing a reasonable change in voltage between light and dark: the multi-purpose operation of the PicAxe's I/O pins. If pin 6 is programmed as digital I/O, it may override whatever voltage is applied externally.
    Make sure in the setup of your program that pin 6 is programmed for ADC input and not for output. Unfortunately I cannot tell you how to achieve this as I'm not familiar with this processor variant.


    A note on the side: Add a small capacitor (10nF or 100nF) parallel to the resistor from pin 6 to ground to increase noise immunity.
    You should also place a 100nF capacitor from the Vcc pin of the PicAxe to the ground pin as close as possible to the chip and an electrolytic capacitor (e.g. 100µF) at the output of the voltage regulator (5V) won't hurt either.
    All these capacitors increase stability of operation of the chip and reduce sensitivity for noise.
     
  12. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    Only X2 parts need directives for configuring a pin for ReadAdc. The 08M2 doesn't need any directives for inputting analog on Pin C.1. The function "ReadAdc C.1,b0" is all that's required. The value that ReadAdc stuffs into the b0 variable can be any value between 0 and 255.

    I wrote a VB6 program a few years ago to convert Voltage values to "B" variables. It makes writing "B" variable values much easier. If you want it I'll dig it out, zip and post it.

    BTW: Since we don't know what model LDR you're using there's absolutely no way of knowing if this code will ever bring C.4 High.

    Code:
    Main:
    
       ReadAdc C.1,b0
          If b0 > 50 then
              High C.4
              Wait 5
          Else
              Low C.4
          EndIf
    GoTo Main
    
    Chris
     
  13. Martaine2005

    Martaine2005

    2,833
    754
    May 12, 2015
    I'll have a copy please Uncle Chris! That is when you have time to dig it out! Not important at all. Any time in this or the next decade!:p

    Marty
     
  14. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    Marty, this app is a simple exe with no dependencies that are not already included in with windows. It does not employ an installer. It's a simple click and run. Let me know if it runs OK on your PC.

    Uncle Chris
     

    Attached Files:

    Last edited: Dec 3, 2015
    Martaine2005 likes this.
  15. Martaine2005

    Martaine2005

    2,833
    754
    May 12, 2015
    Thanks Uncle Chris,
    Works like a charm on Win7... Pretty impressive!!

    The Dark Side is tempting! But I need to lay off the Cookies for now!!:p

    Marty
     
  16. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Ill be back on here tonight or tomorrow to respond on here with some finding and changes.

    Been a pretty busy week.
     
  17. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Hey guys. FInally getting a little time to get back on here.

    So I tested the photocell that I currently have on my design. Im not sure of the model number on it.
    But what I can tell you is that when the light is hitting the cell, the meter reads 27 Mohms.
    When I cover it, it shows O.L Mohms.
     
  18. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    Either it's defective or it's not a typical LDR. They come in many flavors but those measurements are not useful for your purposes. Since you're in the USA you can get damn near anything you want from China on ebay....cheap!

    Right now I can't do it but I'll measure the LDR that Picaxe supplied on my AXE090 proto-board in the morning.

    Chris
     
    Last edited: Dec 23, 2015
  19. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    I measure the LDR without any power applied to it and got a different reading.

    WIth light on the LDR, depending on how close it was to it, was anywhere from 300 ohms to 25 Kohms.

    When I blocked the light, it was anywhere from 150 -200 Kohms.
     
  20. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    Never measure Ohms on a live circuit!

    Chris
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-