---
title: "Using Google Gemini Pro with PICO"
url: "https://maker.wiznet.io/simons/projects/using-google-gemini-pro-with-pico/"
markdown_url: "https://maker.wiznet.io/simons/projects/using-google-gemini-pro-with-pico/md"
type: "WCC: WIZnet Created Content"
author: "simon"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "simon"
license: "MIT license (MIT)"
published: "2023-12-19"
language: "en"
tags: ["MicroPython", "Gemini", "Raspberry Pi", "AI"]
hardware: ["WIZnet W5100S-EVB-Pico"]
likes: 1
views: 2616
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Using Google Gemini Pro with PICO

> Build your application with free API calls

Original author: simon

## Components

- **WIZnet W5100S-EVB-Pico** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5100S/w5100s-evb-pico))
- Software: **MicroPython** ([docs](http://micropython.org/))

## Article

#### Overview

Recently, LLM language models have been popping up in various places, and we've created an example using Google's recently announced Gemini Pro. I used Wiznet Scarlet's OpenAI as a reference for how to use it in pico. Currently, API calls to Gemini Pro are free. Take advantage of this opportunity to build your own simple application.

[Refrence](https://maker.wiznet.io/scarlet/projects/chatting%2Dwith%2Dchatgpt%2Don%2Dwiznet%2Dpico%2Dboard%2Dusing%2Dmicropython/)

![](https://maker.wiznet.io/upload/ckeditor5/20994116%5F1702994560%2Epng)

Currently, Gemini Pro is free for up to 60 calls per minute. API pricing is as above. Considering that the performance is similar to GPT-3.5-turbo, isn't it better to use Gemini for the price and the fact that it's currently free?

#### Software environment

1.Download the Thonny microfiche environment.

![](https://maker.wiznet.io/upload/ckeditor5/20994116%5F1702984232%2Epng)

<https://thonny.org/>

2. Download the Firmware

![](https://maker.wiznet.io/upload/ckeditor5/20994116%5F1702984478%2Epng)

<https://micropython.org/download/W5100S_EVB_PICO/>

3. Google Gemini API key Settings

<https://makersuite.google.com/>

Log in to your Google account and go to the Google AI Studio site above, click new project under Developer Get API key to get a new API key.

![](https://maker.wiznet.io/upload/ckeditor5/20994116%5F1702991623%2Epng)

![](https://maker.wiznet.io/upload/ckeditor5/20994116%5F1702991645%2Epng)

```plaintext
# Quickly test the API by running a cURL command
curl \
  -H 'Content-Type: application/json' \
  -d '{"contents":[{"parts":[{"text":"Write a story about a magic backpack"}]}]}' \
  -X POST https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=YOUR_API_KEY
```

Unlike GPT, the endpoint address contains an api_key, so you'll need to adapt your existing OpenAI code accordingly.

![](https://maker.wiznet.io/upload/ckeditor5/20994116%5F1702991932%2Epng)

Since the form of response is different from GPT, you need to set the indexing code to get the text separately by referring to the docs.

#### Hardware enviroment

Connect the W5xx-EVB-Pico's Ethernet port and the micro5 pin USB(PC,Notebook).

![](https://maker.wiznet.io/upload/ckeditor5/20994690%5F1702991024%2Epng)

Upload the downloaded firmware into 'boot mode' and disconnect and reconnect your USB.

![](https://maker.wiznet.io/upload/ckeditor5/20994116%5F1702991311%2Epng)

Tool -> Interpreters

After connecting, set the interpreter to "Micro Python (Raspberry PI pico)" and the port to the port connected to the device manager earlier.

#### Source Code Setting

https://github.com/jh941213/Gemini_rp2040

```plaintext
!git clone https://github.com/jh941213/Gemini_rp2040.git #download
```

![](https://maker.wiznet.io/upload/ckeditor5/20994690%5F1702992657%2Epng)

Run send.py and the code will run.

#### Gemini.py

```plaintext
import json
import urequests


gemini_api_key = "your_api_key"
gemini_url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key={gemini_api_key}"

def send_prompt_to_gemini(prompt):
    headers = {"Content-Type": "application/json"}
    data = {
        "contents": [{
            "parts": [{"text": prompt}]
        }]
    }
    
    response = urequests.post(gemini_url, headers=headers, data=json.dumps(data))
    if response.status_code == 200:
        response_data = json.loads(response.text)
        return response_data["candidates"][0]["content"]["parts"][0]["text"]
    else:
        raise Exception(f"API error ({response.status_code}): {response.text}")
```

#### Send.py

```plaintext
from machine import Pin, SPI
import network
import utime
import gemini

#init Ethernet code
def init_ethernet(timeout=10):
    spi = SPI(0, 2_000_000, mosi=Pin(19), miso=Pin(16), sck=Pin(18))
    nic = network.WIZNET5K(spi, Pin(17), Pin(20))   # spi, cs, reset pin
    # DHCP
    nic.active(True)

    start_time = utime.ticks_ms()
    while not nic.isconnected():
        utime.sleep(1)
        if utime.ticks_ms() - start_time > timeout * 1000:
            raise Exception("Ethernet connection timed out.")
        print('Connecting ethernet...')

    print(f'Ethernet connected. IP: {nic.ifconfig()}')

def main():
    init_ethernet()

    while True:
        prompt = input("User: ")
        if prompt.lower() == "exit":  
            print("Exiting...")
            break

        try:
            response = gemini.send_prompt_to_gemini(prompt)  
            print("Gemini: ", response)
        except Exception as e:
            print("Error: ", e)

if __name__ == "__main__":
    main()
```

#### Result

You can do this by typing gemini at the prompt in the console window.

![](https://maker.wiznet.io/upload/ckeditor5/20994690%5F1702993401%2Epng)

![](https://maker.wiznet.io/upload/ckeditor5/20994116%5F1703000143%2Epng)

The loop exits when the exit prompt comes in.

---

Source: https://maker.wiznet.io/simons/projects/using-google-gemini-pro-with-pico/
