# Step-By-Setup Setup

This is a Step-By-Step guide to setup, update, and use the Raspberry PiCars. Each Page is a different section, and between them, you can stop and take a breather... You'll need them.
If you want to contact me (Liev) you can E-Mail liev@dorfman.net. I'm happy to help!

# 1. Installing An OS!

<p class="callout warning">**NOTE:** **Any references to the network "StemRoom" should be replaced with "StemSpot"**  
</p>

### Step 1: Prerequisite Software Installation  


- [Raspberry Pi Imager - ](https://www.raspberrypi.com/software/)You'll use this to install the OS on the PiCar, and change some settings to get it connecting. 
    1. Install this for the operating system you're using (Probably Windows).[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/image.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/image.png)[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/64Uimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/64Uimage.png)[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/6o9image.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/6o9image.png)
    2. Once installed, you'll be met with this. Close out for now.[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/scaled-1680-/DAnimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/DAnimage.png)
    3. The OS Installation will be completed later, so hold off on configuration for now.
- [Putty](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) - This is used as a remote terminal for the Pi. Because the PiCar will be roaming without a display, this allows easier access and copy/paste.

1. 1. Installation should be pretty simple. Just click through the prompts, and use the default settings. [![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/UOSimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/UOSimage.png)
        
        [![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/iTRimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/iTRimage.png)[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/70Rimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/70Rimage.png)
        
        [![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/ovcimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/ovcimage.png)[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/R10image.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/R10image.png)
    2. After it gets installed, you can close out of the notepad opened.
2. <p class="callout success">**Travel Router** - This is used to create the network "StemSpot." **While you are configuring, setting up, communicating, and driving the cars, both the client, and car need to be connected to the router.** [More information here: (You'll need to be logged in to access)](https://wiki.pwbstem.club/books/networking/page/network-information-rules-logins-and-passwords "Network Information - Rules, Logins, and Passwords")</p>

### Step 2: Let's Get Started! - Install the OS

Are you ready? Let's put some of those prereqs to work!

1. Open Raspberry Pi Imager
2. Insert a Micro SD Card into the computer with Raspberry Pi Imager, to install Raspberry Pi OS on the SD. 
    1. **The Raspberry Pi Device is "Raspberry Pi 3"**[![SvHimage.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/svhimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/svhimage.png)
    2. The Operating System is **"Raspberry Pi OS (Legacy 64-bit) Lite"**[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/tJ8image.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/tJ8image.png)
    3. Make sure the storage device is the SD card for the PiCar. [![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/CX0image.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/CX0image.png)
        
        [![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/A3Limage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/A3Limage.png)
3. Press "Next", and then Select "Edit Settings"

Copy these settings exactly:

1. **Set Hostname:** PiCar&lt;Num&gt; **(Replace the hostname "PiCarNum" with PiCar followed by the ID (1,2,3, or 4) Ex: "PiCar2")**
2. **Set Username and Password:**
    1. **Username:** pi
    2. **Password:** STEMROOM6041
3. **Configure Wireless LAN:** **[For WiFi passwords, click here](https://wiki.pwbstem.club/books/networking/page/network-information-rules-logins-and-passwords "Network Information - Rules, Logins, and Passwords")** You'll need to be logged into an account with access for it to function.
4. **Set Locale Settings:**
    1. **Time Zone:** America/New\_York
    2. **Keyboard Layout:** us
5. **Services:**
    1. **Enable SSH:** On
    2. **Use Password Authentication:** True
6. **Options:** No Change

[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/DiYimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/DiYimage.png)[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/scaled-1680-/3OSimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/3OSimage.png)

**You can leave the options tab default.**

### Step 3: Revving Up The Pi!

1. You can now remove the SD Card from the computer, and insert it to the Pi.
2. Insert Batteries, connect Pi Power, and wait for it to start up. **Wait 5 Minutes until the next step.**
3. No... I mean it. Set a 5 minute timer, and wait. This is a first startup for the Pi, and you'll have to give it some time to setup the filesystem, and connect to the network.
4. **Make sure the computer you're using is connected to the "StemSpot" network, or all the following steps will not work**.
5. Open Putty 
    1. Under "Host Name (or IP address)" **Enter the hostname you set before.** Ex: PiCar2 [![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/scaled-1680-/Bdqimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-04/Bdqimage.png)
    2. Select Open, and wait for it to connect 
        1. On the off chance it does not connect, you can try setting the IP address of the car using an app like [Fing ](https://www.fing.com/)to scan for the device.
    3. **Enter the username:** pi
    4. **Enter the password:** STEMROOM6041 **Key presses for passwords don't show. After typing, press enter.**
6. You should now see a screen like this:[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/scaled-1680-/BPmimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/BPmimage.png)
7. Update the Pi - To update the pi, there are two separate, but similar commands you will need to enter. Remember to only type one in at a time, and let it run. **You can right-click on the center of the PuTTY window to paste**

```bash
sudo apt update -y
```

```bash
sudo apt upgrade -y
```

<p class="callout success">Now, just reboot the Pi, and the OS will be updated.  
</p>

```bash
sudo reboot now
```

### Step 4: The Nitty Gritty

Now, we'll setup some custom settings and software to get everything running smoothly.

Reconnect to the pi using PuTTY, and use the same credentials

##### Wi-Fi disaster recovery script.

1. Open your home directory ```
    cd ~
    ```
2. Make the script using nano ```
    nano wifirecover.sh
    ```
3. Paste in the following code by copying the text in the computer, and **right clicking on anywhere in the putty window.**  
    ```
    ping -c4 google.com > /dev/null
    
    if [ $? != 0 ]
    then
      echo "No network connection, restarting wlan0"
      /usr/sbin/ifconfig wlan0 down
      sleep 5
      /usr/sbin/ifconfig wlan0 up
    fi
    
    ```
    
    
    1. Use the keyboard combination **Ctrl+X,** Then press **Y**, Then press **Enter**, to exit
4. Now, we'll install **cron**. This lets you schedule tasks for certian times. In this case, it will check for internet connection intermittently. ```
    sudo apt install cron
    ```
5. Crontab uses a text file for configuration, so we'll open it now. ```
    sudo crontab -e
    ```
6. **Select Nano as the text editor**
7. Add this exact line to the end of the text file by copying in the computer, and **right clicking on anywhere in the putty window.**```bash
    1 * * * * /bin/sh /home/pi/wifirecover.sh >/dev/null 2>&1
    ```
8. Use the keyboard combination **Ctrl+X,** Then press **Y**, Then press **Enter**, to exit

<p class="callout success">YIPPPEEEE!!!! Your Operating system is now installed, and **ready to install the PiCar Code!!!**</p>

# 2. Installing the Code!!!

### Part 1: Dependencies

The software relies on other programs to run smoothly. We'll install these now. **If not already, connect using PuTTY.**

1. Install Main Dependencies ```bash
    sudo apt install git python3-xyz python3-pip python3-setuptools python3-flask -y
    
    sudo pip install django-templates
    ```
2. Now, we're going to install the official PiCar-V code. ```bash
    cd ~/
    git clone https://github.com/sunfounder/SunFounder_PiCar-V.git
    ```
3. Enter the directory ```bash
    cd ~/SunFounder_PiCar-V	
    ```
4. Install smaller dependencies ```
    sudo ./install_dependencies
    ```
5. **VERY IMPORTANT!!! REBOOT THE CAR**

```bash
sudo reboot now
```

### Part 2: Extra Settings

In this part, we'll enable I2C in the Pi's Config. This allows you to control the motors. Don't worry, this one's quick, and you're almost ready to drive :}}}}

1. Enter the Raspberry Pi config ```bash
    sudo raspi-config
    ```
2. Select interface options by pressing the number for the option using your keyboard.
3. Enable I2C by moving down to the option and pressing enter.
4. You can **use "esc" to exit.**

### Part 3: Drivers and Services

<p class="callout info">**If not already using the batteries, connect the Pi to the batteries.**</p>

Now, we'll install the drivers for the servos on the car, and we'll write some code to ensure the program runs on boot.

1\. Navigate to the PiCar code directory

```
cd ~/SunFounder_PiCar-V
```

2\. Run the Servo Setup Script - This will move servos in the car, so **make sure it is on the floor in an open space**.

```
picar servo-install
```

3\. This next part sets up AutoStart for the remote control program. We'll make a new "service file" so the pi knows to start it after booting.

```bash
sudo nano /lib/systemd/system/PiCar.service
```

4\. Paste the following code, then exit.

```
[Unit]
Description=PiCarServer
After=multi-user.target

[Service]
Type=idle
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/python3 /home/pi/SunFounder_PiCar-V/remote_control/manage.py runserver 0.0.0.0:8000

[Install]
WantedBy=multi-user.target
```

5\. Now we'll enable the service with the following commands.

```
sudo systemctl daemon-reload
sudo systemctl enable PiCar.service
```

<p class="callout success">DONE! The next page explains how to connect to the car.</p>

# 3. Driving

This is it! You're now ready to drive the PiCar around.

<p class="callout info">**Remember:** Some roaming functionality is not perfect yet. We're working on getting it all up and running.  
</p>

### Connecting...

1. Laptop - Make sure your laptop or client is on: "StemSpot"
2. Using the name of the car, you can connect to it in your browser, like: http://PiCar&lt;num&gt;:8000 **Ex: [http://PiCar3:8000](http://PiCar3:8000)**

<p class="callout info">You can always use the IP if the car name is not working**. Use an app like [fing.](https://www.fing.com/)** </p>

You should be met with this screen. This allows you to control the car, and make necessary calibrations in the settings tab.

[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/scaled-1680-/uEgimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/uEgimage.png)

[![image.png](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/scaled-1680-/iyyimage.png)](https://wiki.pwbstem.club/uploads/images/gallery/2024-02/iyyimage.png)

## **Happy Driving!!!**