{"id":20218966,"url":"https://github.com/juliannicholls/raspberry-pi-misc","last_synced_at":"2025-10-23T20:25:14.740Z","repository":{"id":145398715,"uuid":"41820582","full_name":"JulianNicholls/Raspberry-Pi-Misc","owner":"JulianNicholls","description":"Raspberry Pi programs of various types, including Rpi.gpio, gpiozero, pygame, C, PWM","archived":false,"fork":false,"pushed_at":"2018-06-14T16:45:37.000Z","size":521,"stargazers_count":1,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T13:37:14.107Z","etag":null,"topics":["gpio","motor","raspberry-pi"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JulianNicholls.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-02T18:45:26.000Z","updated_at":"2020-11-23T21:13:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"685220b7-67b9-4079-8e55-d25d263cbb1f","html_url":"https://github.com/JulianNicholls/Raspberry-Pi-Misc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JulianNicholls%2FRaspberry-Pi-Misc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JulianNicholls%2FRaspberry-Pi-Misc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JulianNicholls%2FRaspberry-Pi-Misc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JulianNicholls%2FRaspberry-Pi-Misc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JulianNicholls","download_url":"https://codeload.github.com/JulianNicholls/Raspberry-Pi-Misc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248245065,"owners_count":21071407,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gpio","motor","raspberry-pi"],"created_at":"2024-11-14T06:40:37.517Z","updated_at":"2025-10-23T20:25:14.656Z","avatar_url":"https://github.com/JulianNicholls.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Raspberry Pi Miscellany\n\nFiles from my forays into programming for the [Raspberry Pi](http://raspberrypi.org).\n\nIncludes programs written in C, Python, Javascript, and a little Scratch.\n\nLibraries used: RPi.GPIO, gpiozero, wiringPi, pygame, python-cwiid, and\nsoon Pygame Zero.\n\nIf anyone is interested enough in any of the below programs to read an\nexpanded blog article with diagrams and more code explanation, please \ncontact me and I will write one.\n\n----\n## c\n\nUse `make` to build the programs in here.\n\n### pwm\n\nUses the PWM pin, physical pin 12 (GPIO18). The clock divisor, range, and duty\ncycle value can all be set from a menu. It has been tried with an LED, a\nspeaker, and a 6V motor connected to the collector side of a PN2222 transistor.\n\nI have also tried it with a servo, setting it up for 50Hz (20ms cycle time).\nThe duty cycle need s to be kept very low.\n\nThe frequencies quoted in the program have now been checked with a multimeter that\nhas a frequency function.\n\n\n----\n## python\n\nGeneral python programs.\n\n### test_sleep\n\nTest the resolution of `time.sleep()`.\n\nIt appears to be about 100us (microseconds), which is an order of magnitude \n(or more) better than I expected. time.sleep(n) ALWAYS sleeps for at least \nthe time specified.\n\n\n----\n## python/gpio\n\nA few programs to flash LEDs etc.\n\n### flash18\n\nFlashes an LED attached to Pin 12 (GPIO18).\n\n### flash_12_26\n\nFlashes two LEDs alternately, attached to pins 12 and 26 (GPIO18 \u0026 GPIO7).\n\n### drive_speaker\n\nMakes a 1kHz (ish) tone from a speaker attached to pin 12 (GPIO18).\n\n### drive_lcd\n\nA driver library for LCD displays attached to GPIO pins in a 4-bit configuration\nThe connections have been updated and the pin numbers changed to use BCM\nnumbering (see comments in the file for details).\n\nWhen run directly, it starts by initializing the panel and writing 'SETUP'.\nThen it waits for the user to hit enter between each of the next phases:\nposition the cursor to the beginning of the second line and write\n'Second Line', then write 'End' at the end of the second line, after which\nthe cursor is shown, hidden, and set blinking before finally waiting for\nthe user to hit enter again and then disconnecting from GPIO.\n\n### drive_lcd_20x4\n\nAn updated version of the drive_lcd program, specifically for a 20x4 display.\nThere is more cursor addressing in this one to reflect the extra lines.\n\n### drive_12864\n\nDrive a slightly strange 128x64 display. It seems that most either have an\nI2C or SPI interface, or 8 data bits and 2 chip selects. The one I bought is\ndifferent, and has strange GDRAM addressing instead.\n\nIt turns out that mine would work on SPI too. it works perfectly connected to\nan Arduino using the [U8glib library](https://github.com/olikraus/u8glib).\n\n*See **python/st7920** below*\n\n### lcd_clock\n\nDisplays a clock on the first line of a connected LCD display using the drive_lcd\nlibrary. See drive_lcd for the connection details. It ensures that it always\nreleases the GPIO pins by having a `try...except` round the main loop to trap Ctrl-C.\n\n### delorean\n\nShow the top line from the Delorean display, and the second line is the current\ntime.\n\n### drive_7_segment\n\nDrive a seven segment, common-anode, display. Connections can be seen inside\nthe file. Currently set up for B+, assuming a 40-pin GPIO.\n\nFirst, it shows 0-F with a delay between each. Then, it shows 6 arrows pointing\nforward and back, right and left. Finally, it shows each possible letter and waits\nfor the user to press enter.\n\nThe simplest way to change to a common-cathode display would be to swap the names\nof `segment_on()` and `segment_off()`.\n\n### traffic_lights\n\nSimulate a (British) set of traffic lights with a Red, Amber, and Green LED\nconnected to pins 11, 13, and 15 respectively.\n\n### motor\n\nTurn a motor on and off connected to pin 12 via PN2222A transistor.\n\nI wrote this so that I could turn the motor off quickly in case I'd made a bad\nchoice of transistor or another schoolboy error. The motor is connected to the\ncollector and powered by a wall-wart providing a range of voltages, I tried 6V\nand 9V, but I initially tried it with the 5V line (pin 2) on the Pi GPIO and\nthat was fine too.\n\nit turns out that my choice of transistor was OK. With pin 12 connected via a\n4K Ohm resistor to the Base of the transistor, I'm drawing less than 1mA from\nthe Pi. The motor is drawing around 180mA when running freely. I'm going to try\nstalling the motor a little to test its maximum draw once I have some crocodile\nclips.\n\n### read_ldr\n\nRead a Light Dependent Resistor using the RC charge time of a capacitor. See\ncomments inside for details.\n\nThe circuit I used is a little different from the one suggested in the GPIO Zero\ndocuments for [Light Sensor](https://gpiozero.readthedocs.org/en/v1.1.0/api_input.html#light-sensor-ldr)\n\n### servo\n\nDrive a servo connected to pin 25 using software PWM. I don't think it's very\npractical, but that may be more to do with the low quality servo I'm using. The\nsoftware PCM is wavering between about 47Hz and 51Hz whereas the servo really\nwants a steady 50Hz.\n\nTo drive a servo accurately needs the hardware PCM on GPIO18, I believe.\n\n### clock / clock-ca\n\nDrive a quad 7 segment display as a clock. `clock` drives a common-cathode display\nand `clock-ca` drives a common-anode display. See this \n[Raspi.TV page](http://raspi.tv/2015/how-to-drive-a-7-segment-display-directly-on-raspberry-pi-in-python)\nfor wiring details.\n\n\n----\n## python/gpiozero\n\nExamples using the GPIO Zero library.\n\n### button_led\n\nUsing gpiozero library, switch a LED connected to GPIO17 on and off with a\nbutton connected to GPIO21 and a GND.\n\n### puffin\n\nEmulate a complete British Puffin crossing. It has Red, Amber, and Green LEDs\nfor the cars, obviously. It uses a Red and Green LED for walk / don't walk\nfor pedestrians. It also uses another Red LED to indicate that the crossing\nrequest button has been acknowledged.\n\n### rgd_led\n\nDrive a common-cathode RGB LED.\n\n### robot2\n\nRun a [CamJam Edukit 3 Robot](http://camjam.me/?page_id=1035).\nTakes input from the user:\n\n```\nf   \u003cn\u003e     Forward for n steps\nf   d       Forward until less than 10cm from obstacle\nb   \u003cn\u003e     Backward for n steps\nl   \u003cn\u003e     Left for n steps\nr   \u003cn\u003e     Right for n steps\nq           Quit\n```\n\n### fairy-lights\n\nControl the battery-powered Christmas fairy lights that I bought this year. \nIt randomly fades in and out and flashes at a couple of different speeds.\n\n\n----\n## python/pygame\n\nAdventures in game / UI programming.\n\n### bounce1\n\nBounce a (square!) ball around in a window.\n\n### bounce2\n\nBounce a ball around in a box with a bouncy sound on each bounce. Use the cursor\nkeys to expand and contract the square that the ball is bouncing in.\n\n\n----\n## python/pygame/led_ui\n\nDisplay buttons that turn LEDs on and off. Five buttons turn directly connected\nLEDs on and off. Another seven buttons turn the segments of a display on and off.\nTen more buttons display the digits 0-9 on the 7-segment display and a final 25\nbuttons allow for most of the alphabet in upper or lower case.\n\nSee `main.py` and `.../gpio/drive_7_segment.py` for full connection details.\n\n\n----\n## python/wiimote\n\nExamples using the python-cwiid library.\n[The main CWiiD library information](https://help.ubuntu.com/community/CWiiD).\n\n### wiimote\n\nAn example of using a Wiimote on a Pi, displays the state of the buttons and\nthe accelerometer data. It's a bit flaky with the £3.99 Wiimote clone that I\nbought from eBay, It's a lot more stable with a genuine Nintendo one, the\nrather noisy library can be squelched by using\n\n```\n    wiimote.py 2\u003e/dev/null\n```\n\nif necessary.\n\n### attitude\n\nThe beginning of a program to show the values returned from the accelerometers\nwith the Wiimote in different orientations. I have abandoned it for now\nbecause my Wiimote clone is so flaky that there's no point continuing.\n\n\n----\n## python/st7920\n\nLibrary for a 128x64 ST7920 panel connected via SPI based on\n[Jamie Wood's ST7920 library](http://github.com/JMW95/pyST7920).\n\n### test\n\nTest the library by plotting a single pixel, drawing a rectangle, filled rectangle, \nand reverse filled rectangle, and outputting text which shows how long each redraw\ntakes (~114-118ms on my RPi2).\n\n### show_ip\n\nShow the IP address on the display. This could be used on a headless Pi to show its\naddress to allow logging in via SSH.\n\n\n----\n## python/robot\n\nThese are programs to drive a robot based on the [CamJam Edukit 3](http://camjam.me/?page_id=1035).\n\n### 2-motors\n\nTurn on both motors in one direction for 2 seconds.\n\n### 3-driving\n\nMove forward for a second, right for a second, and then left for a second.\n\n### 4-line\n\nRead the line-following sensor. Returns low (0V) for Black, and high (3.3V)\nfor white. It works at a distance of ~2.5cm (~1\").\n\n### 5-distance\n\nRead the ultrasonic distance sensor. This returns high for the echo flight time.\nThis is then turned into a distance in cm.\n\nA voltage divider is necessary to connect this to a Raspberry Pi because it is a 5V\nperipheral. A 470R and a 330R resistor (provided in the EduKit 3) gives about 3V.\n\n### 6-pwm\n\nUse the software PWM in the `Rpi.GPIO` library to drive forward, right, and left at\nabout 30% power.\n\n### raise_7890\n\nTurn on all 4 lines. I was having trouble with the supplied H-bridge driver board,\nso I needed to turn on all 4 lines that the board uses so that I could connect my\nmultimeter and see if there was voltage, which there wasn't with even one motor\nconnected.\n\n**NB**: *This should not be run with motors connected.*\n\n\n----\n## python/atod-3202\n\nRead a MCP3202 12-bit A/D over SPI.\n\n----\n## i2c\n\nUse the Adafruit I2C library to drive a normal LED connected to the first pin,\nan RGB LED connected to the second, third, and fourth pins on a MCP23017 chip,\nchanging its colour every time a button connected to the fifth I/O pin is pressed.\n\n\n----\n## node\n\nA [Blynk library](http://www.blynk.cc) client program that updates Virtual Pin\n9 with the seconds\nfrom the current time, responds to a slider on V1, updating a graph attached\nto V4, and has a terminal on V3 which feeds back and sends notifications.\n\n\n----\n## scratch\n\nThe beginnings of an alien game. N.B. I can't draw :-)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliannicholls%2Fraspberry-pi-misc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliannicholls%2Fraspberry-pi-misc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliannicholls%2Fraspberry-pi-misc/lists"}