Raspberry Pi

My goal is to use the Raspberry Pi to redo all my previous Arduino projects. Some projects will be much easier with a Pi, and others will be more difficult. I am curious how exactly the two differ!

I will be assuming that you familiar with the technical ideas regarding electronics that were discussed in my Arduino projects, and I will assume a familiarity with Linux. However, if you are a novice, remember that you can learn anything by just playing around and Googling! Perhaps my direct and efficient style of using a Pi will help you learn Linux!

First, you may wish to begin thinking whether you want an Arduino or a Raspberry Pi. In the end, the Pi is more expensive. The Pi is more challenging to use because you have to learn Linux in addition to programming it. The Arduino teaches electronics and C++. The Pi teaches Linux and general programming, but it is somewhat lacking regarding the electronics...
  •  Pi lacks a built-in ADC, so I had to buy one to read analog inputs.
  •  Pi lacks a built-in controllable LED. Putting an LED on pin 13 of the Arduino was a great idea.
  •  Pi cannot be quickly reset, turned on, or turned off. The Arduino has a built-in reset button that almost immediately starts the code from the beginning. However, the Pi has an entire operating system to load, and it might be writing to disk at any moment, so things are more complicated. A built-in reset button does not exist, though you can solder a button to create a soft reset switch, which may be particularly useful if you have programs that run at startup.
  •  When idle, Pi consumes much more power than Arduino. The Arduino has various sleep modes (available to advanced users) that can allow it to run off the same batteries for over a year, but the Pi has no sleep modes at all, so many hours is the best that can be hoped for.
  •  The Pi has no built-in 5V voltage regulator. The Arduino has a built-in voltage regulator (NCP1117) that dissipates extra energy if more than 5 V is applied, though at least 6 V should be applied if using the regulator. Large voltages given to the regulator will cause too much heat or will at least waste much energy, so it's best to directly use 5 V, but it's a nice thing to have.
  •  The Pi has issues. Especially when I am using WiFi instead of Ethernet, my external ADC has a noisy output (I read somewhere that the Pi is not designed to provide clean noise-free power). Also, my audio crackles when using USB speakers (and sometimes my Bluetooth speakers). On a related note, my microSD card might have randomly gotten corrupted because SSH suddenly stopped working (even though I always shut down the Pi before unplugging it), and I sometimes have to restart the Pi when things randomly stop working.
  •  Pi can only provide 16 mA via each of the 3.3V GPIO pins, and no more than 50 mA (or 100 mA for newer models) total through the 3.3V regulator. The Arduino has limits, but the Pi's are more restrictive.
  •  Pi has no real-time processing as the operating system juggles processes, though this often does not matter as it has hardware that controls SPI and I2C and Serial, and its CPU is much faster than the Arduino's.

The Pi has advantages: multiple processes can be run at same time, any computer language can be used, built-in WiFi and Bluetooth, video/audio capabilities, and much storage for things like songs/videos. Recent Arduino boards have built-in WiFi and Bluetooth, which may be the best choice if it meets your specific needs, but, for general tinkering, I always prefer to buy (cheap) external chips like the ESP8266 or HC-05 to get connectivity to my Arduino.

Next, decide if you want to use the Pi as (1) a "normal" computer with desktop screen (perhaps accessed via VNC) including a server or (2) only a server. There are two versions of the operating system Raspberry Pi OS depending on what you want to do. I personally started with Raspberry Pi OS Lite because I already have a "normal" computer and only intend to use the Pi as a controller (I have no need for word processing or other toys/clutter on my Pi). I also like the Lite version because my instructions that work for the Lite will also work of the non-Lite (but non-Lite instructions do not always work on Lite because the Lite needs a bit of extra configuring). However, I now use the desktop version so that I can use VNC instead of SSH if I wish.

I simply have no interest in plugging on a USB mouse, USB keyboard, and HDMI monitor/TV into my Pi. VNC is a very easy and kind of cool way to not need to connect a mouse, keyboard, or monitor, but I even have little interest in VNC because SSH has all the power without the unnecessary show. However, if you somehow finding yourself often wanting to spend money on peripherals, there are touchscreens and wireless keyboards with touchpads.

I am very interested in making the Pi a headless server where SSH is used to control the Pi (no peripherals needed). This way, I can participate in the Internet of Things. To use SSH (or VNC) is the simplest way. You will need to be connected to the same router. Depending on the router configurations, there may be issues if Pi is on the router's guest network, especially if using a computer that is on the router's real network.

Now, buy a Raspberry Pi with WiFi and Bluetooth. This means either the Raspberry Pi model A ($25), model B ($35), or the Zero W ($10). If you might someday want to use the Pi as a "normal" computer, get the Pi Model B to have access to more useful jacks (such as audio, normal HDMI jack, lots of USB, and Ethernet). Because I once was only using Lite, I strongly considered getting the Raspberry Pi Zero W for $10, but certain limitations I found too restrictive: GPIO requires soldering possibly to something like this or this, the single USB data port requires an adapter, and the Pi Zero W has half the SDRAM of the model B and has only a single core (1 GHz) CPU. The real advantage for me is that the Pi Zero W consumes less power than the Pi 3 (useful for robots!), but I wasn't cool enough to go for the Pi Zero W.

For a Raspberry Pi, I needed all of the following to allow my Pi to do all of my Arduino projects (a Pi Zero W would need all of this plus a few extra things to make connections to it)...
  •  Pi 3B and power supply. Newer versions are the Pi 3A+ and Pi 3B+, which gain a minor CPU upgrade, better Bluetooth (BLE), faster Ethernet, and better WiFi (5 GHz in addition to 2.4 GHz). Get a quality regulated-5V Micro-USB power supply that can supply 2.5 A without a noticeable drop in voltage. The Pi can also be powered via the GND and 5V GPIO pins, but only by a well-regulated power supply (this method avoids the Pi's regulator and fuse). Update: the Pi 4B is out, with general improvements, USB-C power plug (instead of Micro-USB), and, to my annoyance, micro-HDMI instead of normal HDMI.
  •  microSD card. The microSD card should ideally be at least class-10, and anything larger than 16 GB is a waste, but storage just keep getting cheaper, so get a 32-GB card for a dollar more.
  •  If your laptop does not have a microSD card reader, and if you don't have a microSD-to-SD adapter to use with an older laptop's SD card reader, get a microSD card reader.
  •  Optional: GPIO breakout for easily using a breadboard. My female jumper wires would not easily connect to the GPIO pins as they are quite thick. If you don't care about preserving the original pin order and powering the breadboard's power rails, this cool thing might be what you want.
  •  ADC for reading analog inputs like the Arduino's analog pins can. You will want at least a 10-bit ADC (210 = 1024). You may not want to use an ADC because the Pi's power is not clean causing small fluctuations, which become a large problem if detecting audio.

All the above cost $80 ($97 after shipping). For a similar price, you can get the equivalent for an Arduino plus lots of electronics (breadboard, jumper wires, sensors, motors, etc.). Don't buy a case because it blocks GPIO pins and makes the Pi get hot, and you can easily make your own (Pi 4B will need this case to be modified a bit).

Getting started

First, install Raspberry Pi OS on the microSD card using the instructions in this link (Etcher is great!). Put an empty file called ssh onto the microSD card to tell the Pi to enable SSH (even the Lite version of Raspberry Pi OS needs this). This file goes into the boot partition. Raspberry Pi OS has a FAT32 boot partition and an ext4 root partition, and non-Linux OS's cannot read ext4, so only the boot partition will mount.

If you have a Pi Zero W or do not have a way to connect your Pi to router via Ethernet, you will need to add another file to the boot partition so that the Pi knows your WiFi setup. Create a text file called wpa_supplicant.conf that reads...

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
	ssid="SkynetGlobalDefenseNetwork"
	psk="password"
}

Be sure to put your actual SSID and passphrase inside the quotes, and change your two-letter country code if not the US.
Now read the following paragraph but ignore the Ethernet stuff.

With the microSD card in the Pi and an Ethernet cable connecting it to your router, turn on the Pi!
While connected to the same router, SSH into it via
   ssh -Y pi@raspberrypi.local
or, on Windows, download and use putty.exe then connect via Pi's local IP address (found by logging into your router), or, now, Windows comes with SSH! On my Android phone (using JuiceSSH app), I also need the local IP address (that is, raspberrypi.local doesn't work). However, depending on your router's DHCP settings, your router may do something called local DNS to resolve phrases like raspberrypi.local to an IP address. Good devices like Linux and macOS—not Windows or certain mobile devices—use something called mDNS to resolve raspberrypi.local to an IP address.
I added the -Y so that graphical programs can be run. If using putty.exe on Windows, do this (install Xming, run Xming, and configure PuTTY) to allow graphical programs to work. I can't get graphical programs to run using the built-in SSH of Windows. If using macOS, you will need to download and install XQuartz (a few years ago, a window popped up giving me a link to download XQuartz when I first tried to run graphical programs remotely).
SSH is now how you will primarily be communicating with the Pi! The password is raspberry. If you haven't done it already, setup WiFi by going to
   sudo raspi-config
then enter SSID and password into NetworkOptions→Wi-fi. Do it again to add another WiFi network (such as a smartphone's hotspot). The Pi 3+ has 5 GHz capabilities in addition to the usual 2.4 GHz! I don't recall if it's necessary, but you may need to restart the Pi via
   sudo reboot
for the WiFi to work. Finally, you can remove the Ethernet cable. Note that, if you cannot use raspberrypi.local to connect and are using the Pi's IP address (because your device is stupid), the local IP address of the Pi's WiFi will be different than the Ethernet local IP address.

You can change the password to something much shorter via
   sudo passwd pi
To setup a few things, go to
   sudo raspi-config
then go to Localization. To upgrade my system, I do
   sudo apt update
   sudo apt upgrade
   sudo reboot

You now have access to a good Bash terminal via SSH, so all you need is a good text editor! I like Leafpad...
   sudo apt install leafpad
   leafpad <file> &
Leafpad will only work if you used the -Y option for SSH (or if you are using VNC to access terminal instead of SSH). If leafpad isn't available, just use geany instead (basically the same usage!).

A few more useful Linux commands...
   exit             ← end SSH session
   sudo halt    ← turn off Pi; do before unplugging
   top             ← view CPU and memory usage
   df -H         ← view disk usage (use -h instead for powers of 1024)
Bash commands are our friends! Here is a Bash tutorial.

Optional: using VNC: Type...
   sudo raspi-config
then go into "Interfacing Options" to enable VNC. Download VNC Viewer on your computer (or mobile device)! Connect to raspberrypi.local or, if that doesn't work, connect to the Pi's local IP address!

Optional: using a smartphone's hotspot: First of all, you do not need mobile data activated for this to work, and the hotspot lets you connect to your Pi wherever you are (if your smartphone is nearby). On my Android, I use the JuiceSSH app for SSH once the Pi is connected to the hotspot. For the Pi to connect to my hotspot, I first have to turn off my apartment's router (because I have both WiFi networks setup in /etc/wpa_supplicant/wpa_supplicant.conf). To get the Pi's local IP address, go to your smartphone's hotspot settings then tap on raspberrypi after it connects. Note that I can connect another computer and the Pi to the hotspot, then that computer can connect to the Pi (JuiceSSH not needed!). At any point, running the commands
   iwconfig
   ifconfig
on the Pi will tell you which WiFi network the Pi is on and the Pi's local IP address, but these commands are not very useful because, if you're connected to the Pi, you already know or don't care!

Optional: making a static IP. Once the router sees the WiFi, I edited the router's DHCP settings to always give the Pi's WiFi the same IPv4 local IP address then rebooted the router. This lets crappy devices that do not support mDNS still easily access the Pi (just remember the IP address!). It also lets me set DMZ in my router to access my Pi from anywhere in the world using my public IP address! Also, this will make using multiple Pis on the same network easier as you can use the now-static local IP addresses instead of variations of raspberrypi.local (or you can change the hostnames of the Pis from raspberrypi via NetworkOptions in sudo raspi-config). If you don't have direct access to the router but have basic information about it, you can instead edit the "interface eth0" via
   sudo nano /etc/dhcpcd.conf

Transferring files

One option is using SCP. The following example is done on the computer connecting to the Pi (not using the SSH into the Pi itself), but it won't work on Windows...
   scp ~/Desktop/test.py pi@raspberrypi.local:~/    ← send test.py to Pi
   scp pi@raspberrypi.local:~/test.py ~/Desktop/    ← receive test.py from Pi
Multiple files can be sent and received using wildcards (such as *).
If the file already exists, SCP will clobber the file (overwrite it) without asking.
SCP can also be run on the Pi, but I'd rather be entering my Pi's password than my real password.
On Windows, download and use WinSCP.exe (I prefer the portable executable).

On Linux, you can use SCP or, better yet, directly mount the Pi! On my Ubuntu, I open the file manager, then go to File→ConnectToServer and enter
   sftp://raspberrypi.local/
Ubuntu will now add this to your list of known connections.

You can also connect USB flash drives to the Pi. After attaching the USB drive, do the following to see where it is mounted...
   lsblk
If it is not automatically mounted, you will have to mount it doing something like...
   sudo mount /dev/sda1 /media/
where /media/ is the mount point. Unmount before physically removing the USB drive...
   sudo umount <mount point>

Python

Python is a terrific language and is the most popular language for the Raspberry Pi. I will be using Python because it is enjoyable to write, and you can always just run the python command to quickly test things out.

A slight complication when using Python is that Python 2 and Python 3 are commonly used, but both versions are incompatible. In 2018, my Raspberry Pi OS came with the python-rpi.gpio package, but I needed to do
   sudo apt install python3-rpi.gpio
for the Python 3 version of the RPi.GPIO module, and I couldn't find Python-3 versions of certain other Python modules. For this reason I used Python 2. More specifically, if you read the top line of any of my Python (.py) files, you will see something like
   #!/usr/bin/env python2.7
which tells Bash to run my code with the python2.7 command.

Using the following commands
   which <command>
   ls -la
I discovered in 2018 using Raspberry Pi OS that python and python2 are links to the python2.7 command, and python3 is a link to python3.5.

If you need faster, more energy efficient code with more control over memory management, use C++. But you will probably never find yourself needing more than Python!

The Arduino uses C++, which is fine because the Arduino software takes care of compilation when you upload the code to the Arduino. Using Python would not make sense because Python usually is not compiled, and a microcontroller running a single process needs compiled code. But Python is more natural for the Pi. Even if you know C++ by using an Arduino, why not learn Python for the Pi?

Actually, use Python 3!

I found all needed Python-3 modules via pip! I now recommend using Python 3!
   sudo apt install python3-pip       ← install pip3
   sudo pip3 install rpi.gpio             ← basic Pi module for controlling GPIO pins
   sudo pip3 install adafruit-mcp3008   ← for the ADC that I described above
   sudo pip3 install pybluez             ← for controlling Bluetooth
I use the sudo in front of the pip3 command so that the Python modules are installed the exact same way that apt would install them, allowing all users to use a single version.

To convert my Python-2 codes to Python 3 in a way that would make them also work with Python 2, I had to do the following...
 -  Rather than print, do print().
 -  When Pi receives raw byte data to be saved to a string, use the decode() method on it.
 -  When the Pi is sending data, make sure that the data is bytes by putting b in front of the data (for example, b"hi" is "hi" as bytes).
 -  When an integer result of division is desired, use // instead of /.
 -  I added bufsize=0 to subprocess.Popen() because the default behavior changed from Python 2 to Python 3. The default of Python 2 was bufsize=0, though, now that I think about it, I am not sure that I want unbuffered in all cases.

The most annoying of the above is how Python 3 handles strings as Unicode text by default (Python 2 treats strings as raw bytes by default). Python 3 is justified in making this change for modern applications, but, to prevent ugly code and headaches, I wish there were some way within a script to switch the default behavior back to raw bytes to easily work with data. However, since we will likely have to switch to Python 3 eventually, I would rather write new codes with Python 3.

Project: Control Pi using WiFi

WiFi was an adventure using an Arduino. Luckily, our Pi 3 or Pi Zero W has WiFi (and Bluetooth) built-in!

Using SSH over WiFi can be very powerful. You have full control over the Pi. You can also control running programs! In the game hit.py, you play by using the numpad. I made it using Python's curses module. Put it in your current directory, then run by doing
   chmod 777 hit.py
   ./hit.py
Running chmod only needs to be done once to change file permissions to executable (run ls -la to see the file permissions of files in current directory).

SSH requires passwords and, if on a mobile device, an app. Instead, you can quickly set up an HTTP server so that you can run programs using any web browser. Just do
   sudo apt install apache2 -y
   sudo apt install php libapache2-mod-php -y
   sudo rm /var/www/html/index.html
   sudo nano /var/www/html/index.php
Then paste the contents of my index.php into yours. To make sure there are never any problems with your new Apache web server not having file permissions, do
   sudo nano /etc/apache2/envvars
then change "export APACHE_RUN_USER=www-data" to "export APACHE_RUN_USER=pi" then run
   sudo service apache2 restart
You can now run relayWiFi.py via any web browser by doing
   http://raspberrypi.local/?state=true
   http://raspberrypi.local/?state=false
to turn GPIO 21 HIGH or LOW. Or, just use my "Arduino" controller, which is a .html file that can be placed on any device. Just enter raspberrypi.local as the "IP address".

Probably a better option is to use Node.js instead of Apache, PHP, and Python! Compared to the W3Schools code, here is my highly improved Node.js code for controlling a robot via the Internet.

Arduino vs. Pi: Pi clearly wins!

Project: Control a relay

To control mains electricity using a contactor relay using relayWiFi.py, see my Arduino instructions here, or just look at my Pi's circuit below.

Click to enlarge
Raspberry Pi controlling a relay

I am using a SRD-05VDC-SL-C for my relay, which is being controlled via a transistor. A diode is used to protect the Pi when the relay's coil turns off.

This would be a great project for a Pi Zero W! Though using just an ESP8266 as shown in my Arduino instructions is cheaper (no microSD card needed). However, the Pi is simpler because it doesn't need all the wires connected everywhere like the ESP8266 does.

As I did with my Arduino, I also want to control the relay by two claps in a row rather than using WiFi. This will be more complicated using a Pi because there is no built-in ADC. My code is relaySound.py, which contains all the connection information in the initial comments. I use the MCP3008 as my ADC and the KY-037 for my sound sensor. See the image below for the connections.

Click to enlarge
Raspberry Pi controlling a relay via sound

Using the ADC introduces a lot of extra wires to make connections. The Arduino was simpler and had a cleaner ADC reading (seems that the Pi's WiFi somehow interfered, whereas Ethernet was better). For both the Pi and the Arduino, the code needs to be improved to prevent background sounds from switching the relay.

The Pi can also use something called I2S for audio. I don't know much about this other than that the instructions for setting up an I2S microphone look complicated, but, once it's setup, Linux can then "see" the microphone, so commands like arecord will work!

In my opinion the best option for having a fully functional microphone would be getting a USB microphone.

Arduino vs. Pi: Pi wins for WiFi control. Arduino wins when using the sound sensor.

Project: Control Pi using Bluetooth

Bluetooth was surprisingly easy using an HC-05 or HC-06 module with an Arduino (not as easy as WiFi on Pi!). There is a paragraph in my ESP8266 discussion about using Bluetooth with an Arduino. Your Pi 3 or Pi Zero W has Bluetooth built-in, but using it is annoying.

Some code to allow an Android app called BlueTerm to control the Pi is bluetoothTest.py. I don't have the Pi do anything interesting because I do not care enough about Bluetooth to be motivated, but this demonstrates the connection by allowing the smartphone and Pi to send messages over Bluetooth. The Python code uses the bluetooth module obtained by
   sudo apt install python3-bluez
Sadly, this module is limited and only seems to work with BlueTerm and not other apps (or perhaps I just don't know how to use it correctly!).

Before this will work, the smartphone and the Pi need to be paired (you just have to do this once). To pair with the Pi as slave, do...
   sudo bluetoothctl
      power on
      pairable on
      discoverable on
Now go to smartphone and pair to Raspberry Pi. Then...
      pairable off
      discoverable off
      quit

As the comments at the top of bluetoothTest.py describe, you can now run the Python code then connect BlueTerm app to the Pi! Then, the Pi can send and receive messages from BlueTerm!

Arduino vs. Pi: Arduino wins due to the simplicity of the HC-05 or HC-06 modules because no commands are needed to pair, and more apps work more easily. But, since the Pi can easily be controlled using SSH and HTTP (for example, using an HTML file or JuiceSSH on a smartphone's hotspot), who would want to control the Pi with Bluetooth?

Project: Audio

A couple of my projects use audio devices, and audio is just fun to play around with on its own!

To get started, run the following to see all of your output and input devices...
   aplay -l
   arecord -l
You will only see devices on card 0 unless you attach more devices via USB, such as these speakers and this microphone. On card 0, there are no recording devices, and there are two output devices: 3.5mm jack and HDMI. You can now display which settings are available for each sound card...
   amixer -c0 contents
   amixer -c1 contents
You can now change things like volume by doing stuff like...
   amixer -c0 cset numid=1 -- -2000
   amixer -c1 cset numid=3 25
Note the extra effort needed to enter a negative number.

Note that, by default, HDMI (including HDMI audio) only works if the connection is made when Pi boots. For my TV, the TV does not need to be turned on when the Pi boots, but the TV needs to be plugged in.

You can play .wav files to your devices via (note that the following .wav file should come with Raspberry Pi OS)...
   aplay -D plughw:0 /usr/share/sounds/alsa/Front_Center.wav
   aplay -D plughw:1 /usr/share/sounds/alsa/Front_Center.wav
To play .mp3 files, omxplayer is crap if you want to play over Bluetooth or via USB speakers (Bluetooth just doesn't work, and USB audio goes way too fast), but I love mpg123...
   sudo apt install mpg123
   mpg123 -a plughw:0 <.mp3 file>
   mpg123 -a plughw:1 <.mp3 file>

To record for 10 seconds in format cd, do something like...
   arecord -D plughw:1 -d 10 -f cd -vv ~/test.wav
To save much disk space, you can convert the .wav to .mp3 by doing...
   sudo apt install lame
   lame test.wav test.mp3
   rm test.wav
Or, never create the .wav in the first place by doing something like...
   arecord -D plughw:1 -d 10 -f cd -vv | lame -m m - test.mp3

To have a voice speak, first do...
   sudo apt install espeak
   espeak --voices
Then, to speak, do something like...
   espeak "Hello world" -v english-us+f5 --stdout | aplay -D plughw:1
I wrote the code espeakTest.py as a joke for my fiancée. She liked the beginning and the end of the conversation.

To play a tone at any frequency for any amount of time, I made a tiny Bash script, beep. You can play different beeps to different cards at the same time by simply running the processes in the background (by ending a Bash command by a space then a &).

To play audio on Bluetooth speakers/earbuds
   sudo apt install bluealsa
You will need to pair the speakers to the Pi (only needs to be done once). The following instructions work for my speakers...
   sudo bluetoothctl
      power on
      scan on
Now make sure device is on discoverable mode, and wait until the device appears. Write down the MAC address (even if you already know the speakers' MAC address, you still have to scan for it). My MAC address is 00:13:EF:00:05:17, but replace it with your own in all the following instructions.
      scan off
      pair 00:13:EF:00:05:17
      connect 00:13:EF:00:05:17
      quit
In the future, connecting will be simpler. Just do
   echo -e "connect 00:13:EF:00:05:17 \nquit" | sudo bluetoothctl
To disconnect, do
   echo -e "disconnect 00:13:EF:00:05:17 \nquit" | sudo bluetoothctl
My Pi will take a few minutes to shut down if I have a connected Bluetooth device that has been turned off, so just run the disconnect command even if the Bluetooth device is already off.
To play a song, do something like one of the following...
   aplay -D bluealsa:HCI=hci0,DEV=00:13:EF:00:05:17,PROFILE=a2dp /usr/share/sounds/alsa/Front_Center.wav
   mpg123 -a bluealsa:HCI=hci0,DEV=00:13:EF:00:05:17,PROFILE=a2dp <.mp3 file>
Since your Bluetooth device is a virtual device, it will not be listed when you run aplay -l.

Finally, let's try some of the stuff we did with the Arduino here. You'll need to alter an audio cord as described in the link. Creating my own sound using bitBangingAudio.py was a huge success! However, responding to audio was tricky due to the noisy power of the Pi preventing clean ADC readings, so I won't even share my code.

Arduino vs. Pi: Pi wins for playing audio. Arduino wins when responding to audio.

Project: Streaming video and audio

At the end of my Arduino audio page, I explain how to use a smartphone alongside an Arduino to be your eyes and ears. Using a smartphone can also be done with a Pi!

Instead, you may wish to use the Pi itself to send video and audio using a USB webcam and USB microphone. I will now explain how to stream audio and video from Pi to a client computer.

The easiest way to send video is to use VNC to connect. Then, in the terminal, run...
   sudo apt install ffmpeg
   ffplay -framerate 15 -video_size 320x240 /dev/video0
Audio does not work over VNC, but video works nicely. You can access VNC over the Internet via your public IP address if your router is setup to send traffic to the Pi.

An even better solution does not need VNC...
   sudo apt install motion
   sudo nano /etc/motion/motion.conf
then edit "stream_localhost" from "on" to "off" so that you can view the webcam remotely. There are other settings such as frame rate and video resolution to play with, but I am happy with the defaults. To start and stop the video server, the easiest way is doing...
   sudo motion &
   sudo pkill motion
You can then access your video from the same network via http://raspberrypi.local:8081/ or, if your router is setup to send traffic to the Pi, from the Internet via http://<your public IP address>:8081/.

As for sending audio from microphone over WiFi, ffmpeg can do this too! Just do something like...
   ffmpeg -f alsa -i hw:1 -acodec mp3 -ab 32k -f rtp rtp://239.0.0.1:5004
except replace the number in hw:1 with the card number shown when you run...
   arecord -l
This works because the IP address is a multicast address. Then, you have to install ffmpeg or VLC media player on your client. On my Mac, I used MacPorts and did...
   sudo port install ffmpeg
   ffplay rtp://239.0.0.1:5004
Or, since I recommend that everyone have VLC media player on their computer, download it and open network rtp://239.0.0.1:5004. I bet you could get this to work over the Internet (assuming the Pi's router and the client's router are setup for it), but I think the Pi would have to specify the correct IP address for a single client.

Now that you understand everything above, it is possible to stream audio and video at the same time. The way that I got this to work was to run VLC media player on Raspberry Pi...
   sudo apt install vlc
   vlc &
I started a stream for audio and video on rtp://239.0.0.1:5004, and, when streaming rtp://239.0.0.1:5004 on my client computer's VLC, it worked! There was a large delay, but I'm sure that certain settings could reduce this.

Arduino vs. Pi: Pi wins for having more options for streaming video and audio.

Project: Snowflake display

I discuss two projects here for the Arduino. The Snowflake display is one of them. To control the Snowflake, you will need to first build the Snowflake display following the instructions in the link, and you'll need something like a 12V power supply in addition to the 5V power supply that powers the Pi.

To have the Snowflake light to "Wizards in Winter" by Trans-Siberian Orchestra, use snowflake.py with the circuit below, where the power supply is on the left, and the Snowflake at the other end of the bundle of white wires.

Click to enlarge
Raspberry Pi controlling Snowflake display

To have the Snowflake respond to any sound, use snowflakeSoundSensor.py with the circuit below. The comments at the beginning of the code contain connection details for the ADC and sound sensor. The sound sensor and ADC are the same that I used when controlling a relay (see above). Because my ADC is only 10-bit and I don't have an amplifier, the sound sensor has to be placed very close to the speakers producing the sound.

Click to enlarge
Raspberry Pi and sound sensor controlling Snowflake display

Arduino vs. Pi: Pi wins for being able to easily control audio! Arduino wins when using the sound sensor.

Project: RUN Display

Of the two projects here for the Arduino, the RUN display is the second I have now done with the Pi. You will need to first build the RUN display following the instructions in the link. Then, use RUN.py.

You may use a motion detector to activate the RUN display with the circuit below (the motion detector is the thing on the right).

Click to enlarge
Raspberry Pi controlling RUN display with motion detector

You may use a photoresistor to activate the RUN display with the circuit below. The breadboard is a real mess, but it works well! Somewhere in the middle is the ADC. To the upper right of the ADC largely hidden by a wire is the tiny photoresistor. The comments at the top of RUN.py should be helpful in connecting the ADC and photoresistor.

Click to enlarge
Raspberry Pi controlling RUN display with photoresistor

The motion detector I used (HC-SR501) works well with the 3.3 V of the Pi. The detector is powered by 5V but outputs a 3.3V signal. The photoresistor also works wonderfully with the Pi. Just connect it to 3.3 V instead of the 5 V used for the Arduino.

However, the RUN display must be powered using 5 V rather than 3.3 V because the limitations of the Pi's 3.3V regulator. 5 V worked wonderfully with the Arduino and can also work well with the Pi. The Arduino only needed a single transistor to control 23 of the 26 letters, but the Pi needs 4 transistors because the R, U, and N are at 5 V and cannot be directly controlled with 3.3 V. At first, I had hope that I wouldn't need the extra transistors because the resistance of LEDs is very high when not fully lit so the 3.3V pins might be protected from the 5 V, but the red LED on the N lit somewhat even when the 3.3V output pin was HIGH (it fully lights when the 3.3V output pin was LOW), so I quickly removed power as I was certainly damaging the Pi by having current flow from 5V into the 3.3V output pin. Luckily, there was an LED to light up so I could see my stupidity (I never should have tried it!). Using 4 transistors is certainly the best choice for the Pi, because I can then use the same resistances calculated for the Arduino, and I don't have to worry about ever sending current into 3.3V pins. I used 2000-Ω resistors for controlling the R, U, and N transistors, and I used a 220-Ω resistor for controlling the transistor for the remaining 23 letters. Important: do not use less than 220 Ω on Pi's 3.3V pins.

Arduino vs. Pi: Arduino wins for being able to easily handle 5V and analog-to-digital conversion.

Project: Control Christmas-tree lights

Once I do this with an Arduino, I'll do it with the Pi! The code is treeLights.py. With my SSR module, I can test out my code because the module has an LED for each SSR. See the connections below.

Click to enlarge
Raspberry Pi controlling Christmas tree lights using SSR relay module

Note that, with 4 channels on, the relay module draws 80 mA from the 5V of the Pi (20 mA per channel). Luckily, the 5V does not pass through the 3.3V regulator, so this is fine.

Arduino vs. Pi: Just like controlling the Snowflake Display, the Pi works much better because the Pi can easily control audio!