读取树莓派上GPIO引脚的温度传感器数值的步骤。

huangapple go评论111阅读模式
英文:

Reading Temperature sensor values on GPIO's of RPi. steps

问题

我是Go语言的初学者,请告诉我如何在树莓派的GPIO上读取温度传感器的值,并将读取的模拟信号转换为数字值以显示电压。

英文:

I am beginner to Go language, please provide me steps to read temperature sensor values on gpio of rpi. and how to process the read analog signal in to digital values for displaying voltage.

答案1

得分: 1

从http://www.rpiblog.com/2012/11/reading-analog-values-from-digital-pins.html

>>"不幸的是,树莓派的所有17个引脚都是数字引脚,只能输出高电平或低电平。但是通过使用一个简单的电路(穷人的模拟/数字转换器),您可以使用单个GPIO引脚测量多个电平值。"

您可以按照该指南的类似方法进行操作,或者将AD转换器连接到您的树莓派上。

使用Dave Cheney的gpio包读取值的实际过程包括将引脚设置为input模式(示例代码都来自于watch示例)。

要将引脚设置为input模式:

pin, err := gpio.OpenPin(gpio.GPIO22, gpio.ModeInput)
if err != nil {
	fmt.Printf("打开引脚时出错!%s\n", err)
	return
}

然后,通过在输入引脚上调用BeginWatch()来获取高电平或低电平的值:

err = pin.BeginWatch(gpio.EdgeFalling, func() {
	fmt.Printf("%d的回调被触发!\n\n", gpio.GPIO22)
})
if err != nil {
	fmt.Printf("无法监听引脚:%s\n", err.Error())
	os.Exit(1)
}

然后,可以使用第一个链接中概述的过程处理这些值,然后您需要创建正确的输出信号来驱动数字显示器(这些可能因型号、功能和其他因素而有很大差异。您需要查找您使用的显示器的参考资料)。

要将引脚设置为output模式:

power, err := gpio.OpenPin(gpio.GPIO17, gpio.ModeOutput)
if err != nil {
	fmt.Printf("打开引脚时出错!%s\n", err)
	return
}

您可以使用Set()设置输出引脚的值为高电平,使用Clear()设置为低电平:

power.Set()
power.Clear()

使用完毕后不要忘记使用Close()关闭引脚。

pin.Close()
power.Close()
英文:

From http://www.rpiblog.com/2012/11/reading-analog-values-from-digital-pins.html

>>"Unfortunately all the 17 pins of Raspberry Pi are digital which can either output HIGH or LOW. But by using a simple circuit (poor man's A/D converter) you can measure multiple level of values using a single GPIO pin."

You can either do something similar to that guide, or connect an AD converter to your Raspberry Pi.

The actual process of reading the values using Dave Cheney's gpio package consists of setting the pin to input mode (Example code is all from the package watch example)

To set a pin to input mode:

pin, err := gpio.OpenPin(gpio.GPIO22, gpio.ModeInput)
if err != nil {
	fmt.Printf("Error opening pin! %s\n", err)
	return
}

You would then get the HIGH or LOW values by calling BeginWatch() on the input pin:

err = pin.BeginWatch(gpio.EdgeFalling, func() {
	fmt.Printf("Callback for %d triggered!\n\n", gpio.GPIO22)
})
if err != nil {
	fmt.Printf("Unable to watch pin: %s\n", err.Error())
	os.Exit(1)
}

The values would then be processed using the procedure outlined in the first link, and then you would have to create the correct output signal to drive the digital display (These can differ greatly depending on model, capability and many other things. You'll need to look up the reference for the display you're using).

To set a pin to output mode:

power, err := gpio.OpenPin(gpio.GPIO17, gpio.ModeOutput)
if err != nil {
	fmt.Printf("Error opening pin! %s\n", err)
	return
}

You would write the values to the output pins using Set() for HIGH and Clear() for LOW:

power.Set()
power.Clear()

Don't forget to Close() the pins after use.

pin.Close()
power.Close()

huangapple
  • 本文由 发表于 2013年8月29日 12:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/18502479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定