Learn how to use the SPI data bus with Arduino in chapter thirty-four of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A seemingly endless tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here. 

[Updated 10/01/2013]

This is the first of two chapters in which we are going to start investigating the SPI data bus, and how we can control devices using it with our Arduino systems. The SPI bus may seem to be a complex interface to master, however with some brief study of this explanation and practical examples you will soon become a bus master! To do this we will learn the necessary theory, and then apply it by controlling a variety of devices. In this tutorial things will be kept as simple as possible.

But first of all, what is it? And some theory…

SPI is an acronym for “Serial Peripheral Interface”. It is a synchronous serial data bus – data can travel in both directions at the same time, as opposed to (for example) the I2C bus that cannot do so. To allow synchronous data transmission, the SPI bus uses four wires. They are called:

  • MOSI – Master-out, Slave-in.This line carries data from our Arduino to the SPI-controlled device(s);
  • MISO – Master-in, Slave out.This line carries data from the SPI-controlled device(s) back to the Arduino;
  • SS – Slave-select.This line tells the device on the bus we wish to communicate with it. Each SPI device needs a unique SS line back to the Arduino;
  • SCK – Serial clock.

Within these tutorials we consider the Arduino board to be the master and the SPI devices to be slaves. On our Arduino Duemilanove/Uno and compatible boards the pins used are:

  • SS – digital 10. You can use other digital pins, but 10 is generally the default as it is next to the other SPI pins;
  • MOSI – digital 11;
  • MISO – digital 12;
  • SCK – digital 13;

Arduino Mega users – MISO is 50, MOSI is 51, SCK is 52 and SS is usually 53. If you are using an Arduino Leonardo, the SPI pins are on the ICSP header pins. See here for more information. You can control one or more devices with the SPI bus. For example, for one device the wiring would be:

Data travels back and forth along the MOSI and MISO lines between our Arduino and the SPI device. This can only happen when the SS line is set to LOW. In other words, to communicate with a particular SPI device on the bus, we set the SS line to that device to LOW, then communicate with it, then set the line back to HIGH. If we have two or more SPI devices on the bus, the wiring would resemble the following:


Notice how there are two SS lines – we need one for each SPI device on the bus. You can use any free digital output pin on your Arduino as an SS line. Just remember to have all SS lines high except for the line connected to the SPI device you wish to use at the time.

Data is sent to the SPI device in byteform. You should know by now that eight bits make one byte, therefore representing a binary number with a value of between zero and 255. When communicating with our SPI devices, we need to know which way the device deals with the data – MSB or LSB first. MSB (most significant bit) is the left-hand side of the binary number, and LSB (least significant bit) is the right-hand side of the number. That is:

Apart from sending numerical values along the SPI bus, binary numbers can also represent commands. You can represent eight on/off settings using one byte of data, so a device’s parameters can be set by sending a byte of data. These parameters will vary with each device and should be illustrated in the particular device’s data sheet. For example, a digital potentiometer IC with six pots:

sdata

This device requires two bytes of data. The ADDR byte tells the device which of six potentiometers to control (numbered 0 to 5), and the DATA byte is the value for the potentiometer (0~255). We can use integers to represent these two values. For example, to set potentiometer number two to 125, we would send 2 then 125 to the device.

How do we send data to SPI devices in our sketches?

First of all, we need to use the SPI library. It is included with the default Arduino IDE installation, so put the following at the start of your sketch:

Next, in void.setup() declare which pin(s) will be used for SS and set them as OUTPUT. For example,

where ss has previously been declared as an integer of value ten. Now, to activate the SPI bus:

and finally we need to tell the sketch which way to send data, MSB or LSB first by using

or

When it is time to send data down the SPI bus to our device, three things need to happen. First, set the digital pin with SS to low:

Then send the data in bytes, one byte at a time using:

Value can be an integer/byte between zero and 255. Finally, when finished sending data to your device, end the transmission by setting SS high:

Sending data is quite simple. Generally the most difficult part for people is interpreting the device data sheet to understand how commands and data need to be structured for transmission. But with some practice, these small hurdles can be overcome.

Now for some practical examples!

Time to get on the SPI bus and control some devices. By following the examples below, you should gain a practical understanding of how the SPI bus and devices can be used with our Arduino boards.

Example 34.1

Our first example will use a simple yet interesting part – a digital potentiometer (we also used one in the I2C tutorial). This time we have a Microchip MCP4162-series 10k rheostat:


Here is the data sheet.pdf for your perusal. To control it we need to send two bytes of data – the first byte is the control byte, and thankfully for this example it is always zero (as the address for the wiper value is 00h [see table 4-1 of the data sheet]).  The second byte is the the value to set the wiper, which controls the resistance. So to set the wiper we need to do three things in our sketch…

First, set the SS (slave select) line to low:

Then send the two byes of data:

Finally set the SS line back to high:

Easily done. Connection to our Arduino board is very simple – consider the MCP4162 pinout:

Vdd connects to 5V, Vss to GND, CS to digital 10, SCK to digital 13, SDI to digital 11 and SDO to digital 12. Now let’s run through the available values of the MCP4162 in the following sketch:

Now to see the results of the sketch. In the following video clip, a we run up through the resistance range and measure the rheostat value with a multimeter:

Before moving forward, if digital potentiometers are new for you, consider reading this short guide written by Microchip about the differences between mechanical and digital potentiometers.

Example 34.2

In this example, we will use the Analog Devices AD5204 four-channel digital potentiometer (data sheet.pdf). It contains four 10k ohm linear potentiometers, and each potentiometer is adjustable to one of 256 positions. The settings are volatile, which means they are not remembered when the power is turned off. Therefore when power is applied the potentiometers are all pre set to the middle of the scale. Our example is the SOIC-24 surface mount example, however it is also manufactured in DIP format as well.

To make life easier it can be soldered onto a SOIC breakout board which converts it to a through-hole package:

ad5204boardss

In this example, we will control the brightness of four LEDs. Wiring is very simple. Pinouts are in the data sheet.pdf.

ex34p2schematic

And the sketch:

The function allOff()and allOn()are used to set the potentiometers to minimum and maximum respectively. We use allOff() at the start of the sketch to turn the LEDs off. This is necessary as on power-up the wipers are generally set half-way. Furthermore we use them in the blinkAll() function to … blink the LEDs. The functionsetPot() accepts a wiper number (0~3) and value to set that wiper (0~255). Finally the function indFade() does a nice job of fading each LED on and off in order – causing an effect very similar to pulse-width modulation.

Finally, here it is in action:

class="youtube-player" type="text/html" width="420" height="315" src="http://www.youtube.com/embed/QYFfX4VAabQ?version=3&rel=0&fs=1&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent" frameborder="0" style="border-width: 0px; margin: 0px; padding: 0px;">

Example 34.3

In this example, we will use use a four-digit, seven-segment LED display that has an SPI interface. Using such a display considerably reduces the amount of pins required on the micro controller and also negates the use of shift register ICs which helps reduce power consumption and component count. The front of our example:

7segfrss

and the rear:

7segrearss

Thankfully the pins are labelled quite clearly. Please note that the board does not include header pins – they were soldered in after receiving the board. Although this board is documented by Sparkfun there seems to be issues in the operation, so instead we will use a sketch designed by members of the Arduino forum. Not wanting to ignore this nice piece of hardware we will see how it works and use it with the new sketch from the forum.

Again, wiring is quite simple:

  • Board GND to Arduino GND
  • Board VCC to Arduino 5V
  • Board SCK to Arduino D12
  • Board SI to Arduino D11
  • Board CSN to Arduino D10

The sketch is easy to use, you need to replicate all the functions as well as the library calls and variable definitions. To display numbers (or the letters A~F) on the display, call the function

where a is the number to display, b is the base system used (2 for binary, 8 for octal, 10 for usual, and 16 for hexadecimal), and c is for padded zeros (0 =off, 1=on). If you look at the void loop() part of the example sketch, we use all four number systems in the demonstration. If your number is too large for the display, it will show OFfor overflow. To control the decimal points, colon and the LED at the top-right the third digit, we can use the following:

After all that, here is the demonstration sketch for your perusal:

And a short video of the demonstration:

So there you have it – hopefully an easy to understand introduction to the world of the SPI bus and how to control the devices within. As always, now it is up to you and your imagination to find something to control or get up to other shenanigans. In the next SPI article we will look at reading and writing data via the SPI bus.

LEDborder

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitter, Google+, subscribe  for email updates or RSS usng the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Arduino and the SPI bus相关推荐

  1. PX4模块设计之四十一:I2C/SPI Bus Instance基础知识

    PX4模块设计之四十一:I2C/SPI Bus Instance基础知识 1. 基础知识 2. 基础类和定义 2.1 ListNode类 2.2 List类 2.3 BusCLIArguments类 ...

  2. Arduino应用开发——spi flash(以esp32和w25qxx为例)

    Arduino应用开发--spi flash 目录 Arduino应用开发--spi flash 前言 1 硬件介绍 1.1 模块简介 1.2 硬件连接 2 软件开发 2.1 寄存器介绍 2.2 编程 ...

  3. Arduino UNO通过SPI串行方式驱动LCD12864液晶屏

    LCD12864液晶屏简介 LCD12864带中文字库图形点阵式液晶显示器,可配合各种单片机可完成中文汉字.英文字符和图形显示,可构成全中文人机交互图形界面,模块具有功耗低.显示内容丰富等特点而应用广 ...

  4. Python学习手册(致敬谢公子)

    目录 基础语法 模块的使用 Python案例 基础语法 PyCharm调试程序 python手动安装依赖包 python中让输出不换行 Python中的输入(input)和输出打印 python中实现 ...

  5. Arduino SPI + SPI Flash芯片W25Q80BV

    W25Q80BV是台湾华邦电子(Winbond)生产的8M-bit串行flash芯片.主要特性有: 工作电压:2.5 ~ 3.6 V 功耗:读写(active)时4mA,低功耗(power-down) ...

  6. Arduino使用u8g2库函数驱动4线/6线OLED屏幕(I2C/SPI通讯)附带库函数详解

    话不多说,直接入正题: 常见的OLED通常有两种样式,如下图所示,分别是4线和6线控制 本人几乎浏览了网上所有关于控制OLED模块的教程,并都经过了项目实测:大力推荐u8g2这个库函数来控制,文章末尾 ...

  7. Raspberry Pi 与Arduino SPI通信

    本教程介绍了使用SPI(串行外围设备接口总线)进行Raspberry Pi与Arduino通讯和控制的基本框架. SPI代表了一种非常完善的芯片间通信方法,该方法在两种设备的硬件中均实现. 在这里,我 ...

  8. arduino智能浇花系统_arduino+水泵+继电器+RFID

    arduino+继电器+电机 应用场合:加湿器.自动浇花.智能门锁.报警系统.......总之很多场合都适用.本章就介绍利用RFID卡输入,驱动水泵. /* * ------------------- ...

  9. arduino tft 方向_ESP32在Arduino环境下玩转 LVGL,ESP32移植LVGL详细教程

    微信关注 "DLGG创客DIY"设为"星标",重磅干货,第一时间送达. ❝ 转载自慕容流年 https://me.csdn.net/qq_41868901 ❞ ...

最新文章

  1. 初中计算机基础知识说课稿,计算机基础知识说课稿
  2. 线性筛法 与 线性求欧拉函数 的计算模板
  3. inline修饰虚函数问题
  4. cmstop中实例化controller_admin_content类传递$this,其构造方法中接收到的是--名为cmstop的参数--包含cmstop中所有属性...
  5. pipreqs 组件
  6. HDU-1255 覆盖的面积 矩形面积交
  7. 有商在线进销存成功案例
  8. linux 扫描媒体库,如何扫描出Android系统媒体库中视频文件
  9. 与JavaWeb有关的故事(Web请求与Java IO)
  10. windows server上通过关闭端口有效防治勒索病毒
  11. 北京专精特新企业申报攻略
  12. mysql emoy表情_emo表情包 - emo微信表情包 - emoQQ表情包 - 发表情 fabiaoqing.com
  13. Java抽象类、接口理解
  14. 工作队列模式(任务队列)| RabbitMQ系列(二)
  15. TensorFlow基础学习
  16. UCOS II移植到STM32F103开发板
  17. anchors.fill和anchors.centerIn区别
  18. Jarvis-OJ WEB 多题writeup
  19. 文科生本科专业能学计算机吗,请问高中文科生到大学里学“软件技术”这个专业,上课能听懂,跟得上么?...
  20. Arduino 四针脚声音传感器

热门文章

  1. 前端学习-CSS京东导航栏
  2. 一篇文章构建你的 NodeJS 知识体系(W字长文)
  3. Arcgis使用教程(十)ARCGIS地图制图之经纬网格添加
  4. 传统企业借力商派助推转型
  5. 美赛论文Latex简易模板 | 快速上手(附注释)
  6. 网站在线监控工具Statping
  7. 初窥江湖之PhotoShop抠图(二)
  8. 家居装修选购:挑选家用沙发的8个禁忌
  9. 如何生成二维码?生成二维码其实很简单
  10. python软件下载对电脑配置要求-Python实现的读取电脑硬件信息功能示例