> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/general/development-on-crowpi/hardware-and-interfaces-usage-cli/lab-usage-gpio-via-cli.md).

# LAB: Usage GPIO via CLI

## Usage GPIO via CLI

การใช้คำสั่ง commade line ในการควบคุม GPIO ของ CrowPi นี้จะยึดตัวเลข pin ตามแบบ GPIO.BCM&#x20;

**คำสั่ง:** การเข้าถึงสิทธิ์ root ในบอร์ด

```typescript
sudo -i  #enable root account privileges 
cd /sys/class/gpio
```

**คำสั่ง:** กำหนดค่า pin GPIO เพื่อเรียกใช้งานผ่าน CLI

```typescript
echo 21 > export #GPIO21 which is Pin 40cd gpio21
```

**คำสั่ง:** กำหนดหน้าที่ของ pin GPIO เป็น Output&#x20;

```typescript
echo out > direction
```

**คำสั่ง:** ใช้ cat สำหรับแสดงค่าในไฟล์ direction&#x20;

```typescript
cat direction
```

**คำสั่ง:** กำหนดค่าให้กับ pin GPIO เป็น high

<pre class="language-typescript"><code class="lang-typescript"><strong>echo 1 > value 
</strong></code></pre>

**คำสั่ง:** กำหนดค่าให้กับ pin GPIO เป็น low

```typescript
echo 0 > value 
```

**คำสั่ง:** ใช้ cat สำหรับแสดงค่าในไฟล์ value     &#x20;

&#x20;    สังเกตุที่ pin 40 บน CrowPi LED จะติด value เป็น 1 และจะดับเมื่อเป็น 0

```typescript
cat value 
```

**คำสั่ง:** กำหนดค่า pin กลับไปค่าเริ่มต้น

```typescript
cd ..echo 23 > unexport 
```

## Bash Script

```bash
# usage:
# >sudo sh gpio.sh <pin> <value>
# writing example:
# >sudo sh gpio.sh 21 1

#!/bin/bash

#assign parameters
pin=$1
value=$2

#create gpio instance
echo $pin > /sys/class/gpio/export
#assign direction
echo out > /sys/class/gpio/gpio$pin/direction
#assign value
echo $value > /sys/class/gpio/gpio$pin/value
#remove gpio instance
echo $pin > /sys/class/gpio/unexport
```

&#x20;

ตัวอย่าง code from Bash script to read and write RaspberryPi gpio pin

<pre class="language-typescript"><code class="lang-typescript"><strong>git clone https://gist.github.com/175dd01a80af9c237da2ce90f103bc14.git script
</strong></code></pre>

## **Ex1: การใช้งาน Raspberry Pi ควบคุม GPIO ของบอร์ด Crow pi ผ่าน CLI**

&#x20;    กำหนด Raspberry Pi สั่งควบคุม Buzzer บนบอร์ด Crow Pi ซึ่งจากบอร์ด Crow Pi ระบุว่าเชื่อมต่ออยู่กับ Pin 12 ที่ยืดตามเลขแบบ GPIO.Board&#x20;

Date: May 2023

Author: Soontree Jaikhong (AIC-Researcher)

Author: Thanaluk Pranekunakol (AIC-Researcher)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.aic-eec.com/general/development-on-crowpi/hardware-and-interfaces-usage-cli/lab-usage-gpio-via-cli.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
