Learning electronic and programming with Raspberry Pi
The easiest way to run with raspberry is installing NOOBS in a FAT32 SD. Other OS like Ubuntu Mate, Raspbian, or Chromium are valid too. PD: At present, all OS are 32 bits.
First step must be change default password, for security reasons, like ssh access. Use “sudo passwd” command. Username: pi Password: raspberry
Later, update the system:
sudo apt-get update
sudo apt-get upgrade
If raspbian freezes, disconnect usbs and HDMi can help. Disconnect supply could corrupt system files. Use ctrl+alt+f1 to switch to kernel, and use sudo reboot. To return to GUI, use ctrl+alt+f7.
For Arduino installation:
sudo apt-get install arduino
For Opencv installation:
`sudo apt-get install libhdf5-dev libhdf5-serial-dev` `sudo apt-get install libqtwebkit4 libqt4-test` `sudo pip install opencv-contrib-python`
With GPIO pins, two different numerations can be used: BOARD (with the numerical position for each pin), and BCM, the special numeration of broadcom. But this last form is not the same for every raspberry model.
When program finish, using a keyboard interruption or execution ends, ports used to remain in their last state, and that can be problem. These lines cleans all ports used before. Because of that, don’t use it at the beginning.
`def main():` `while True:` `...` `if __name__ == '__main__':` `try:` `main()` `except KeyboardInterrupt:` `...` `finally:` `GPIO.cleanup()`
Run a script with rc.local or cron.
sudo nano /etc/rc.local
At the end of the file, but before the exit 0
, put the command that must be run during start, with absolute paths:
python3 /home/pi/example.py &
If the command runs continuously or is likely not to exit, you must be sure to fork the process by adding an ampersand to the end of the command, &
. Otherwise, the script will not end and the Pi will not boot. The ampersand allows the command to run in a separate process and continue booting with the process running.
First, enable ssh access on Interfaces.
Obtain the Raspberry IP, or make it static:
hostname -I
Access throught PC terminal with ssh:
ssh pi@<IP>
If you want to use GUI programs:
ssh -Y pi@<IP>
Now you can, for example:
idle3 &
For file transfer between machines, use scp
:
scp "name with spaces.txt" pi@<IP>:
With VNC, Don’t use :1
when raspberry uses desktop interface, it will appear a grey screen. Use :2
.
SD cards tend to get corrupted easily, for problems with supply during writing commands. Because of that, use the raspberry in read-only mode. The script includes a reboot for kernel panics, but these are not the only problems that can freeze the raspi. Because of that, activate the watchdog. The timer limit is 15 s.
modprobe bcm2835_wdt
echo "bcm2835_wdt" | sudo tee -a /etc/modules
apt-get install watchdog
update-rc.d watchdog defaults
nano /etc/watchdog.conf
Uncomment:
#watchdog-device
#max-load-1
Add:
watchdog-timeout = 15
Save, reboot, test by command:
:(){ :|:& };:
Temporary files are stored in RAM rather than on the SD card. Optionally, you can use a jumper or switch to boot the system into normal read/write mode to install new software or data. And, as normal, you still have easy access to the /boot partition if the SD card is mounted on another computer.
- This does not work with the X11 desktop / PIXEL. It's strictly for Raspbian Lite right now. Graphical applications are still possible using SDL, Pygame and so forth, just not X11 at the moment.
- Setting up read-only mode should be the very last step before deploying a project. THIS SEQUENCE IS IRREVERSIBLE. There is no uninstall script. There’s an option to boot into read/write mode, but nothing to back out all these changes.And back up the contents of your SD card first.
Pi should be booted and on the network… like mentioned above, everything already configured and fully functional (and backed up) before taking this step.
From a command line prompt:
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/read-only-fs.sh
sudo bash read-only-fs.sh
The script will repeat all these stern warnings and make you verify at several steps whether to continue. Along the way you’ll be presented with a few options:
Enable boot-time read/write jumper? [y/N]
This gives you the option to run the system in read/write mode by inserting a jumper across two pins. If you answer yes to this question, you’ll also be asked for a GPIO pin number. When there’s a jumper between this pin and ground, the system will boot into read/write mode and you can make changes (but remember to do a proper shutdown). Make sure the pin isn’t used by anything else. GPIO21 is easy to remember because it’s right at the end of the header. If you’re using I2S audio though, that requires GPIO21 for its own use, so you’ll want to pick another.
Install GPIO-halt utility? [y/N]
This installs a utility that initiates a proper shutdown when another GPIO pin is touched to ground. For a read-only system, you probably don’t need this… but if you have the system booted in read/write mode, this provides an option if you can’t log in and run a manual shutdown. This likewise will ask for a GPIO pin number like GPIO16.
You’re Not Finished Yet. Test the modified system to make sure that the system boots and your application runs as intended. Try a pass with the read/write jumper and/or the gpio-halt button, if you’ve enabled either of those options.
First we have to increase max output current through USBs to avoid hubs like in the picture.
Add the following line to the bottom of /boot/config.txt
max_usb_current=1
Now install usb mode switch to change the dongle function
apt-get install usb-modeswitch usb-modeswitch-data
Create /etc/udev/rules.d/70-usb-modeswitch.rules with the following content:
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="1f01", RUN+="/usr/sbin/usb_modeswitch -v 0x12d1 -p 0x1f01 -V 0x12d1 -P 0x1405 -J"
This line is for Huawei E3372 and E3531 models. Others dongle may need different IDs. Now then raspi should detects a eth connection.
-
lsusb before udev rule:
root@raspberrypi:~# lsusb
Bus 001 Device 003: ID 12d1:1f01 Huawei Technologies Co., Ltd.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
-
lsusb after udev rule:
root@raspberrypi:~# lsusb
Bus 001 Device 003: ID 12d1:14dc Huawei Technologies Co., Ltd.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
To avoid wifi connections during boot, we have to set down the wlan, editing rc.local:
sudo nano /etc/rc.local
sudo ifconfig wlan0 down
-
Buttons polling: with a digital read loop
-
Control buttons with events: use pin events to handle push buttons
-
Piano button: play sound files throught audio jack with pygame libraty. But warning, use .wav files with 16 bits and 128 bps. Other formats can be used throught other linux program with
subprocess
. Play sounds on RBpi with a piezo hasn’t much sense because the easiest way is use pwm and with it we lose jack audio output. One way is use audio utils, 2, vlc, or piGame, to play audio files. We can synthesize midi audio with Sonic pi, or PyGame midi. -
Keypad: control the classic 3x3 and 4x4 keypads. Only install the library with
sudo pip3 install pad4pi
-
Arduino analog read: it'ns better to connect an arduino than use a ADC module.
-
Arduino I2C communication: remember to enable the IwC and SPI interfaces on the RBpi, and nstall i2c tools to know the devices address:
sudo apt-get install i2c-tools
Install smbus to use i2c pins and rebootapt-get install python-smbs
-
LCD 2004 I2C: only install raspberry LCD library:
sudo pip3 install RPLCD
-
Ultrasonic sensor: For precise measurement better use C. Remember use a resistor divisor for echo pin
-
Hardware PWM: There is only two harware PWM channels on the Raspberry pi, that can be access on GPIOs 12, 13, 18 and 19 (32, 33, 12, and 35). 12 and 18 share same setting as 13 and 19. However, this pwms are us for audio, and we loose them with audio play. We can replace it with software pwm, but is less time precise, maybe for leds application only.
-
Picamera: enable camera with piconfig and connect the ribbon cable to the camera connector, (not the display connector, you absent minded). The camera preview only works when a monitor is connected to the Pi, so remote access (such as SSH and VNC) will not allow you to see the camera preview.