Raspberry Pi运行Arduino Sketch

在本文中,您将学习如何在Raspberry Pi上运行为Arduino编写的sketch。 这将使我们能够将Arduino代码编译为可以在Raspberry Pi上运行的二进制文件。 但是在我们这样做之前,我们必须在Arduino IDE和Raspberry Pi上做一些准备。

硬件

软件

Arduino IDE设置

Raspberry Pi设置

基本示例:LED和控制台

理论已经足够,需要一些练习的时间! 我们将以最基本的Arduino功能开始工作:LED和串行输出。 我们将使用以下Arduino Sketch,这是经过稍微修改的“ Fading” Arduino示例。 使用PWM(脉冲宽度调制)设置LED亮度,并将当前的PWM值打印到控制台。

我们有了代码,现在我们需要将所有组件连接在一起。这里也没有什么复杂的,只有两个LED和10k电阻。

现在,所有控制台输出将出现在控制台中。 与其他任何终端程序一样,可以通过键盘快捷键Ctrl + C停止该程序。 我更喜欢手动上传,因为它不需要设置其他服务,并且通常更可靠(至少在我的设置下)。

使用以上两种方法之一上载sketch,并观察其中一个LED闪烁,另一个则逐渐淡入和淡出!

sketch还将当前的PWM值打印到控制台中,这就是输出的样子。

高级示例:SPI和I2C

显然,您可以通过闪烁的LED和控制台输出来完成出色的工作,但是我认为,当添加一些更高级的功能(例如I2C和SPI)时,您可以做的事情更多。 RasPiArduino框架也支持这些框架,因此我们可以使用I2C总线将Raspberry连接到许多不同的传感器,或者使用SPI总线连接到通信模块之类的东西。 对于I2C设备,我将使用ROHM传感器评估套件中的BH1745NUC颜色传感器,对于SPI设备,我将使用SX1278 LoRa模块。 它们都在3.3V逻辑上运行,因此将它们与Raspberry连接就像连接几根电线一样容易。

这就是电路的样子。它虽然不漂亮,但可以完成工作。

在此示例中,所有重要信息都被打印到Raspberry控制台。 我们可以看到SX1278成功接收到包含字符串“ Hello Raspberry!”的数据包。 (从Arduino通过LoRenz屏蔽发送),并且颜色传感器正在测量正确的值!

详情参阅 - 亚图跨际

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Program Arduino with ease! Using clear, easy-to-follow examples, Programming Arduino: Getting Started with Sketches reveals the software side of Arduino and explains how to write well-crafted sketches using the modified C language of Arduino. No prior programming experience is required! The downloadable sample programs featured in the book can be used as-is or modified to suit your purposes. Understand Arduino hardware fundamentals Install the software, power it up, and upload your first sketch Learn C language basics Write functions in Arduino sketches Structure data using arrays and strings Use Arduino's digital and analog inputs and outputs in your programs Work with the Standard Arduino Library Write sketches that can store data Program LCD displays Use an Ethernet shield to enable Arduino to function as a web server Write your own Arduino libraries In December 2011, Arduino 1.0 was released. This changed a few things that have caused two of the sketches in this book to break. The change that has caused trouble is that the classes 'Server' and 'Client' have been renamed to 'EthernetServer' and 'EthernetClient' respectively. To fix this: Edit sketches 10-01 and 10-02 to replace all occurrences of the word 'Server' with 'EthernetServer' and all occurrences of 'Client' with 'EthernetClient'. Alternatively, you can download the modified sketches for 10-01 and 10-02 from here: http://www.arduinobook.com/arduino-1-0 Make Great Stuff! TAB, an imprint of McGraw-Hill Professional, is a leading publisher of DIY technology books for makers, hackers, and electronics hobbyists.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值