element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Open Source Hardware
  • Technologies
  • More
Open Source Hardware
Forum Use Go lang to control GPIO on pcDuino
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Open Source Hardware to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 0 replies
  • Subscribers 315 subscribers
  • Views 290 views
  • Users 0 members are here
  • lang
  • go
  • pcduino
Related

Use Go lang to control GPIO on pcDuino

alvin_jin
alvin_jin over 12 years ago

In this post, we show how to use Go lang to control GPIO on pcDuino.

If you have updated the image, sometimes, GPIO is not loaded by default. We need to use commands to load GPIO:

  1. $sudo modprobe gpio
  2. $sudo modprobe adc
  3. $sudo modprobe pwm

The main program is as follows:

  1. package main
  2. import (
  3. "fmt"
  4. "./gpio"
  5. "time"
  6. )
  7. func main() {
  8. g, err := gpio.NewGPIOLine(7,gpio.OUT)
  9. if err != nil {
  10. fmt.Printf("Error setting up GPIO %v: %v", 18, err)
  11. return
  12. }
  13.       blink(g, 100)
  14.       g.Close()
  15. }
  16. func blink(g *gpio.GPIOLine, n uint) {
  17. fmt.Printf("blinking %v time(s)\n", n)
  18. for i := uint(0); i < n; i++ {
  19. g.SetState(true)
  20. time.Sleep(time.Duration(1000) * time.Millisecond)
  21. g.SetState(false)
  22. time.Sleep(time.Duration(1000) * time.Millisecond)
  23. }
  24. }

The package used is shown below:

  1. package gpio
  2. import (
  3.      "fmt"
  4.      "os"
  5. )
  6. type GPIOLine struct {
  7.      Number uint
  8.      fd     *os.File
  9. }
  10. const (
  11.      IN = iota
  12.      OUT
  13. )
  14. func NewGPIOLine(number uint, direction int) (gpio *GPIOLine, err error) {
  15.      gpio = new(GPIOLine)
  16.      gpio.Number = number
  17.         err = gpio.SetDirection(direction)
  18.      gpio.fd, err = os.OpenFile(fmt.Sprintf("/sys/devices/virtual/misc/gpio/pin/gpio%d", gpio.Number), os.O_WRONLY|os.O_SYNC, 0666)
  19.      if err != nil {
  20.           return nil, err
  21.      }
  22.      return gpio, nil
  23. }
  24. func (gpio *GPIOLine) SetDirection(direction int) error {
  25.      df, err := os.OpenFile(fmt.Sprintf("/sys/devices/virtual/misc/gpio/mode/gpio%d", gpio.Number), os.O_WRONLY|os.O_SYNC, 0666)
  26.      if err != nil {
  27.           return err
  28.      }
  29.      fmt.Fprintln(df, "out")
  30.      df.Close()
  31.      return nil
  32. }
  33. func (gpio *GPIOLine) SetState(state bool) error {
  34.      v := "0"
  35.      if state {
  36.           v = "1"
  37.      }
  38.      _, err := fmt.Fprintln(gpio.fd, v)
  39.      return err
  40. }
  41. func (gpio *GPIOLine) Close() {
  42.      gpio.fd.Close()
  43. }

 

The code can be downloaded from pcduinogo.tar

Please download the above code to “$HOME/mygo”. To run the code, we do “$go run blink.go”.

IMG_2574

IMG_2572

IMG_2573

  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube