Clayton Lambert has written a nice Python script which takes a picture with the camera module and scans it for changed pixels, indicating motion. It uses the picamera library and I think it’s particularly good because you can see how it looks at each pixel of the image and counts the changes. He explains it in the video above and the code is available here.