Notifications
Clear all

ArduCam and libcamera

132 Posts
6 Users
4 Likes
5,711 Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7021
 

@davee Excellent advice Dave. I would go a step further, and use C rather than Python as 1 line of Python is who knows how many lines of C and as such much is hidden and therefore you can't really learn the basic foundations.

I am not sure how exactly to do this on a Pi, but in the Arduino IDE there is an example sketch for the Pi PICO called Button. Maybe copy that and get it working, then simply change the button for a PIR sensor and observe. I will do the same with a PICO and my PIR sensor and post the results.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

@emeyeraway

Are your looking at the Radomnerd example to have a go at incorporating the PIR into your program, or is it for some other reason.   I do have a PIR somewhere and it would not take very long for me to put get it working with the example program, and I happy to give it a go as long as I know exactly what you are trying to achieve.   

In the meanwhile I will have a look at the program you posted and see if theres a quick fix to be applied.  But if its just a case of adding a PIR to the example code I gave then that would be easy to do.

 


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

@emeyeraway

My dear chap, very well done, I've not hooked up a PIR but without it your amendments to the radomnerd code works a treat, apart from the preview screen shows me upsidedown  😀 

So now I think it will be easy enough to amend both your radomnerd based code, or my example for use with a pir.  It does point to something on your PIR rather than your code being at fault though, so it would be well to backtrack and look again at the advice proffered by @zander and @davee regarding the sensor.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

@emeyeraway

I've not found my PIR as yet, I sort of remember I had it hooked up to an arduino, so I must have dumped into some storage box, I will probably find it later.  

But I did find an easy to hook up push button that had handy dupont connectors already attached so I put it on pin 2 like your demo code, but made it take a picture in place of the PIR.  I see an error in you code, the take picture statement should be - take_photo() - you forgot the brackets 😀   so my test code for the button to take a picture is button.when_pressed = take_photo().  To I've now got a lovely picture of my head where my waist should be.

All a PIR does, or should do, is just to make a pin go high on motion, just like the button press, but I will continue to seek out my PIC after my evening meal to check that at least one PIR is behaving itself.

Posted by: @zander

I would go a step further, and use C rather than Python

Tish tish Ron, when you only have a hammer, every problem look like a nail. 😎 

This post was modified 1 year ago by byron

   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

@emeyeraway

I found a PIR, in fact 4 of the little things.  It works just fine.   But I note that mine has a jumper setting for single or repeat triggering, and two mini potentiometers to adjust the block time etc.   See the screen prints below for my PIR.   So your code is just fine (apart from the () 👍 .  It your PIR or your adjustment of your PIR thats the problem.

Screenshot 2023 02 21 at 20.38.35
Screenshot 2023 02 21 at 20.39.58
This post was modified 1 year ago by byron

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7021
 

@emeyeraway @byron I now have my PIR connected to the example button sketch wired to a PICO. Rather than a camera, I am using a LED as the output device. Here are the results.

  1. Jumper in L position
    1. Wave in front of PIR, wait for LED start stopwatch
    2. LED goes out, stop stopwatch, time = 30 secs
    3. Wave in front of PIR, wait for LED start stopwatch
    4. At the 15 sec mark wave in front of stopwatch again
    5. LED goes out, stop stopwatch, time = 30 secs
  1. Jumper in H position
    1. Wave in front of PIR, wait for LED start stopwatch
    2. LED goes out, stop stopwatch, time = 30 secs
    3. Wave in front of PIR, wait for LED start stopwatch
    4. At the 15 sec mark wave in front of stopwatch again
    5. LED goes out, stop stopwatch, time = 45 secs

As expected according to the datasheet the H position resets the start time for every detection, while the L position does not. For the game camera, bird feeder scenarios, the H is the better option.

HOWEVER, the application must use a timer to set how often to take a picture with maybe a counter to limit the number of pictures to prevent hogging or filling the card.

In other words, after taking a picture, delay for x seconds and add 1 to a counter. If counter exceeds my max, delay for 1hr?

In the case of L there is a on-board RC circuit that controls the time between stop and start of 2 seconds I think. This could be changed if you want to mess with SMD devices.

My PIR has 2 pots, one for distance or sensitivity, one for ON duration. Setting these is common sense. I suggest you get one like mine. I have no idea as to model number but it shouldn't be hard to figure out. Start with Bill's video, or search Amazon to find one with adjustments.

In the case of using Python, I will need to experiment further but it must either incorporate a state machine or it will take pictures without stopping unless logic is added similar to a C implementation.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

Posted by: @zander

In the case of using Python, I will need to experiment further but it must either incorporate a state machine or it will take pictures without stopping unless logic is added similar to a C implementation.

@emeyeraway  We must say thank you to Ron as he was on the right track from the start, its the PIR that done it.  His PIR is probably more sophisticated than my mini one and may be more like the one you have.

But lets have Ron do his state machine in python too, its about time  he got into a modern computer language 😀 (though a hint, just just set a picture limit on a continued motion trigger, and do a count of pictures taken).   I expect you want to do your own coding for this but give a shout with your requirements if you need any further assistance.  But I may also use Ron's state-machine in my code 👍 

This post was modified 1 year ago by byron

   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

@emeyeraway  

Woops, you should NOT put those brackets in as I suggested for the gpiozero stuff.  I was confusing myself by putting in the follow code when testing 

button.when_pressed = take_photo('button')
pir.when_motion = take_photo('motion') 

and printing out what action took the photo.  It worked, sort of,  a whole bunch of pictures resulted, and I stupidly ignored the quantity, too overcome by the fact the button and PIR was working. 😊 

But of course for the gpiozero module a reference to the function should be given not a function call.  Sorry for any confusion.  Just in case I give the exact code I finally successfully tested, which is basically your code but I put all the pictures in the same directory as the code so that they appeared in the Thonny files panel for ease of testing the code.   So no brackets... silly me.  And in the code my pins that connect the PIR and the button are different to yours so beware of that, but of course it makes no difference which ones you use.

from picamera2 import Picamera2, Preview
#Project 13 - Burglar Detector With Photo Capture
#latest code updates available at:   https://github.com/RuiSantosdotme/RaspberryPiProject  
#project updates at:   https://nostarch.com/RaspberryPiProject  

#import the necessary packages
from gpiozero import Button, MotionSensor
from signal import pause
from picamera2 import Picamera2
from libcamera import Transform
import time

#create objects that refer to a button,
#a motion sensor and the PiCamera
button = Button(21)
pir = MotionSensor(15)
picam2 = Picamera2()

#start the camera
#picam2.rotation = 180
picam2.start_preview(Preview.QTGL, transform=Transform(hflip=True, vflip=True))

def file_name():
    t = time.localtime()
    current_time = time.strftime("%Y%m%d  %H:%M:%S", t)
    return current_time + ".jpg"

#stop the camera when the pushbutton is pressed
def stop_camera():
    picam2.stop_preview()
    print('exit the program')
    exit()

#take photo when motion is detected
def take_photo():
    picam2.capture_file(file_name())
    print('A photo has been taken')
    # max photos 1 every 10 seconds 
    time.sleep(10)

picam2.start()
time.sleep(2)


#button.when_pressed = stop_camera
button.when_pressed = stop_camera
#assign a function that runs when motion is detected
pir.when_motion = take_photo

pause()
This post was modified 1 year ago by byron

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7021
 

@byron @emeyeraway I copied the above code to my Pi and it does bring up the preview window. Of course, as a game camera even if only in the backyard we don't want a preview, we want a picture so I will do a little work on the script to take the picture and if needed build in some constraints as discussed earlier as in time between pictures, and the total number of pics per 'session'.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

@byron @emeyeraway

Ron, I think the eme may well want a preview, and he did state the preview was not working for him in one of his first postings.   If the camera is, say, attached to the house and looking at the garden or at the driveway for unwanted visitors then a preview would be rather useful.  

I understand a game camera, or one on battery power would be a different story.  If you dont want a preview, then simply dont put it in the code.  (just a couple of deletes and your there).

What I would do is to have a program with a preview that has the ability to adjust all the settings so as to set the camera up properly (the defaults have to cover a wide range of camera modules and you wont get the best pic with the defaults)  You can then use the adjusted settings for your preview-less snaps in another program, or the same program where you can switch the preview on/off with a button press.

You cant get away with just deleting the couple of places in the code that call the preview screen.  We expect much better than that. 😀 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7021
 

@byron So you think he is going to sit in front of his computer screen all day waiting for some animal to trip the camera? I certainly hope that isn't what he wants, that would be a monumental waste of both his time and technology. This is the perfect example of letting technology do the heavy lifting. Of the millions of game cameras installed world wide I doubt there are very many connected to a laptop a few feet away with a human watching the screen.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7021
 

@byron How do I rotate the camera -90 or +270 degrees? My camera is mounted in the 7" display and there is only one way to mount it. I checked the picamera2 pdf manual and searched on rotate but nothing useful was found. I am either misreading the doc or it's a missing feature which I find hard to believe.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7021
 

@byron @emeyeraway @davee I found the picamera2 documentation. WOW, there is a lot to learn. Start at page 108 for gpiozero. I am curious about the following arguments

queue_len, sample_rate, threshold, partial

There is a lot more to learn, I have yet to see any code that works in any sensible way.

I have spent more time on this than I meant to so I will watch to see if anybody comes up with a full script that actually works in a sensible matter.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

Posted by: @zander

@byron So you think he is going to sit in front of his computer screen all day waiting for some animal to trip the camera? I certainly hope that isn't what he wants, that would be a monumental waste of both his time and technology. This is the perfect example of letting technology do the heavy lifting. Of the millions of game cameras installed world wide I doubt there are very many connected to a laptop a few feet away with a human watching the screen.

Come on Ron, put your thinking cap on.  Having a preview screen, switchable on/off with a push button does not mean you can't have a PIR does it.   You set the camera up, probably at base camp, but maybe in the wild with a connected laptop, then switch off the preview screen, ensure the camera is operating in its minimal current draw mode, remove the usb connection, secure the watertight box, and go home to have dinner.   I think you must have had a long day.

 

 


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

Posted by: @zander

@byron How do I rotate the camera -90 or +270 degrees? My camera is mounted in the 7" display and there is only one way to mount it. I checked the picamera2 pdf manual and searched on rotate but nothing useful was found. I am either misreading the doc or it's a missing feature which I find hard to believe.

you mount your camera on one of these (see below) though that seems rather incomparable with your desire not to have a preview ability.  The code to move it concerns moving servos so why would that me in the libcamera doc.

https://thepihut.com/products/pan-tilt-hat

I expect you really meant how can you flip or transpose the picture (left to right, top to bottom).  That all in the picamer2 manual.  See how the preview screen is flipped in the code snippet.

 

This post was modified 1 year ago by byron

   
ReplyQuote
Page 8 / 9