Thicket data repository for the EEG
1{
2 "id": "https://www.tunbury.org/2021/01/06/raspberry-pi-camera-with-m12-lens",
3 "title": "Raspberry PI Camera with M12 Lens",
4 "link": "https://www.tunbury.org/2021/01/06/raspberry-pi-camera-with-m12-lens/",
5 "updated": "2021-01-06T12:41:29",
6 "published": "2021-01-06T12:41:29",
7 "summary": "I really need a good lens on my Raspberry PI camera to use it with OBS from a decent distance. The new high resolution Rasperberry PI cameras look excellent but they also come with a heafty price tag which I just can’t justify.",
8 "content": "<p>I really need a good lens on my Raspberry PI camera to use it with OBS from a decent distance. The new high resolution Rasperberry PI cameras look excellent but they also come with a heafty price tag which I just can’t justify.</p>\n\n<blockquote>\n <p>First off, the mounting holes on both v1 and v2 RPi cameras are on 21 mm centers, so the 20 mm spacing of the M12 mount you link isn’t a perfect fit. Depending on your mounting screw size, you may still be able to force it. Second, you have to manually cut or file down a notch in the M12 mount for the micro-flex cable that comes out of the camera module. That isn’t too hard, but if you want, there is also a M12 mount specifically designed for the RPi cameras, with a notch already.</p>\n\n <p>The v1 and v2 sensor sizes are the same, the so-called 1/4-inch format. On V1 the lens focal length is f=3.6mm with Angle of View: 54 x 41 degrees and on V2 it is f=3.0mm with Angle of View: 62.2 x 48.8 degrees [1]. Note the angle of view is quoted at full-frame; remember some video modes use a cropped subset of the full frame. This is a moderately wide angle lens. If you double the focal length, you’ll get half the field of view. If you get a 8mm lens that’s a moderate telephoto, and a 16mm lens is definitely telephoto. I’ve tried a number of cheap M12 lenses that work “ok” but don’t expect perfectly sharp images with the tiny 1.4 or 1.1 micron pixels these camera sensors use. Lower f-number lenses are “faster” (let in more light) but will have more shallow depth of field and more blurry overall. You will see f/1.4 or lower sold for use in low light, but I have not had good images with those; I would recommend f/2.0 or above if you want decent resolution.</p>\n\n <p><a href=\"https://www.raspberrypi.org/forums/viewtopic.php?t=150344#p988445\">https://www.raspberrypi.org/forums/viewtopic.php?t=150344#p988445</a></p>\n</blockquote>\n\n<p>With that as the inspiration I bought a pack of ten M12 lens adapters from Amazon for £5 and started out by creating a notch for the cable. While the 20mm spacing wasn’t ideal I have found some variation in hole positions on the PCB and by using thin M2 bolts I was able to <em>force</em> them.</p>\n\n<p>I removed the lens in a rather destructive way from the front of the camera by cutting around the raised area on three sides with a craft knife. It wasn’t pretty but it did the job.</p>\n\n<p><img alt=\"\" src=\"https://www.tunbury.org/images/pi-camera-m12-1.jpg\"></p>\n\n<p>On the first camera I modified I went on to remove the IR filter by gently cutting it across the diagonal with side cutters. Surprisingly it popped off without too much effort leaving this.</p>\n\n<p><img alt=\"\" src=\"https://www.tunbury.org/images/pi-camera-m12-2.jpg\"></p>\n\n<p>For my application, removing the IR filter was a mistake as (tungsten) lights and candles produce lots of infrared!</p>\n\n<p>I mounted the M12 adapters on 3mm plywood with short M2 bolt screwed in from the front.</p>\n\n<p><img alt=\"\" src=\"https://www.tunbury.org/images/pi-camera-m12-3.jpg\"></p>\n\n<p><img alt=\"\" src=\"https://www.tunbury.org/images/pi-camera-m12-4.jpg\"></p>\n\n<p>I had an old Foscam WiFi camera which has an M12 lens marked as <em>f=2.8mm</em>. This pretty much matched the field of view I got from the camera’s native lens.</p>\n\n<p>I have had good results with <em>f=8mm</em>, <em>f=15mm</em> and <em>f=25mm</em> lens as well as cheap zoom lens offering a range of <em>f=3mm</em> to <em>f=12mm</em>. It’s curious that on Amazon a focal length of 8mm is typically sold as <em>wide angle</em> rather than telephoto! What I really notice is that the depth of field becomes increasingly narrow as the focal length increases.</p>\n\n<p>I installed Raspberry Pi OS Lite using the Pi Imager and enabled SSH before removing the SD card.</p>\n\n<p>After assembling the unit check that the camera is connected up and enabled with <code>vcgencmd get_camera</code></p>\n\n<div><div><pre><code>supported=1 detected=1\n</code></pre></div></div>\n\n<p><code>raspivid</code> can be configured to send an h.264 stream, but it exits when the connection drops. Therefore, I have rolled <code>raspivid</code> as a service so systemd will restart it each time.</p>\n\n<p>Create <code>/etc/systemd/system/stream.service</code> containing</p>\n\n<div><div><pre><code>[Unit]\nDescription=auto start stream\nAfter=multi-user.target\n\n[Service]\nType=simple\nExecStart=/usr/bin/raspivid -v -fps 30 -md 2 -n -ih -t 0 -l -stm -fl -o tcp://0.0.0.0:5001\nUser=pi\nWorkingDirectory=/home/pi\nRestart=always\n\n[Install]\nWantedBy=multi-user.target\n</code></pre></div></div>\n\n<p>Enable and start the service as follows:</p>\n\n<div><div><pre><code>systemctl enable stream\nservice stream start\n</code></pre></div></div>\n\n<p>You can open the stream with VLC by using the address <code>tcp/h264://192.168.1.88:5001</code> which is useful for testing.</p>\n\n<p>Finally in OBS connect add a media source <code>tcp://192.168.0.88:5001</code>.</p>\n\n<p><img alt=\"\" src=\"https://www.tunbury.org/images/2_8mm.png\" title=\"f=2.8mm\">\n<img alt=\"\" src=\"https://www.tunbury.org/images/8mm.png\" title=\"f=8mm\">\n<img alt=\"\" src=\"https://www.tunbury.org/images/16mm.png\" title=\"f=16mm\">\n<img alt=\"\" src=\"https://www.tunbury.org/images/22mm.png\" title=\"f=22mm\"></p>\n\n<h1>Parts list</h1>\n\n\n\n \n \n Part\n Cost\n \n \n \n \n <a href=\"https://www.amazon.co.uk/Raspberry-Pi-Model-Quad-Motherboard/dp/B01CD5VC92\">Pi 3B</a>\n £34\n \n \n <a href=\"https://www.amazon.co.uk/gp/product/B07WCGY2QY/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1\">PoE Splitter - 2 pack</a>\n £17\n \n \n <a href=\"https://www.amazon.co.uk/gp/product/B07ZZ2K7WP/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1\">5MP Camera Module - 2 pack</a>\n £9\n \n \n <a href=\"https://www.amazon.co.uk/gp/product/B08FDVYC98/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1\">Zoom lens</a>\n £10\n \n \n <a href=\"https://www.amazon.co.uk/gp/product/B00R1J42T8/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1\">M12 Mount - 10 pack</a>\n £5\n \n \n <a href=\"https://www.amazon.co.uk/gp/product/B075QMCYZM/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1\">3mm plywood - 25 pack</a>\n £24\n \n \n <a href=\"https://www.amazon.co.uk/gp/product/B003WIRFD2/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1\">SD Card</a>\n £3.70\n \n \n\n\n<p>A single camera would cost £62.</p>",
9 "content_type": "html",
10 "author": {
11 "name": "Mark Elvers",
12 "email": "mark.elvers@tunbury.org",
13 "uri": null
14 },
15 "categories": [
16 "raspberrypi",
17 "obs"
18 ],
19 "source": "https://www.tunbury.org/atom.xml"
20}