paolo carnovalePeter Oakes
Published

IOT2020 Using an Arduino Sketch including I2C, SPI and PWM

We have all seen the videos and marketing up to now with how the IOT2020 can be programmed by using an Arduino IDE.

BeginnerFull instructions provided2 hours2,860
IOT2020 Using an Arduino Sketch including I2C, SPI and PWM

Things used in this project

Hardware components

SIMATIC IOT2020
Siemens SIMATIC IOT2020
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

An Arduino Sketch for the IOT2020 used to drive 2 I2C displays, an 8 digit 7 segment LED display

Arduino
#include <SPI.h>
#include <Wire.h>
#include <LiquidCrystal_I2C_8574.h>
#include <SeeedOLED.h>
#include <avr/pgmspace.h>

LiquidCrystal_I2C_8574 lcd(0x20, 16, 2); // set the LCD address to 0x20 for a 16 chars and 2 line display

//const byte SS = 10;  // omit this line for Arduino 1.0 onwards
const byte MAX7219_REG_NOOP        = 0x0;
// codes 1 to 8 are digit positions 1 to 8
const byte MAX7219_REG_DECODEMODE  = 0x9;
const byte MAX7219_REG_INTENSITY   = 0xA;
const byte MAX7219_REG_SCANLIMIT   = 0xB;
const byte MAX7219_REG_SHUTDOWN    = 0xC;
const byte MAX7219_REG_DISPLAYTEST = 0xF;

const int AnalogPin = 9;
void sendByte (const byte reg, const byte data)
{
  digitalWrite (SS, LOW);
  SPI.transfer (reg);
  SPI.transfer (data);
  digitalWrite (SS, HIGH);
}  // end of sendByte

void setup ()
{
  SPI.begin ();
  sendByte (MAX7219_REG_SCANLIMIT, 7);      // show 4 digits
  sendByte (MAX7219_REG_DECODEMODE, 0xFF);  // use digits (not bit patterns)
  sendByte (MAX7219_REG_DISPLAYTEST, 0);    // no display test
  sendByte (MAX7219_REG_INTENSITY, 3);      // character intensity: range: 0 to 15
  sendByte (MAX7219_REG_SHUTDOWN, 1);       // not in shutdown mode (ie. start it up)

  // init 16x2 LCD display on I2C 0x20
  lcd.init();                      // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Hello from!");
  lcd.setCursor(0, 1);
  lcd.print("The Breadboard");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Counter:-");

// init OLED 128x64 display
  SeeedOled.init();                   //initialze SEEED OLED display
  SeeedOled.clearDisplay();          //clear the screen and set start position to top left corner
  SeeedOled.setNormalDisplay();      //Set display to normal mode (i.e non-inverse mode)
  SeeedOled.setPageMode();           //Set addressing mode to Page Mode
  SeeedOled.setTextXY(0,0);          //Set the cursor to Xth Page, Yth Column  
  SeeedOled.putString("Hello viewers"); //Print the String
  SeeedOled.setTextXY(3,0);          //Set the cursor to Xth Page, Yth Column  
  SeeedOled.putString("youtube");
  SeeedOled.setTextXY(4,0);          //Set the cursor to Xth Page, Yth Column  
  SeeedOled.putString("thebreadboardca"); //Print the String

  pinMode(AnalogPin, OUTPUT);
}   // end of setup


void number (const int num)
{

  char buf [8];
  sprintf (buf, "%8i", min (max (num, 0), 99999999));
  lcd.setCursor(0, 1);
  lcd.print(buf);

  SeeedOled.setTextXY(6,0);          //Set the cursor to Xth Page, Yth Column  
  SeeedOled.putString(buf); //Print the String

  analogWrite(AnalogPin, num & 0XFF);
  
  // send all 8 digits
  for (byte digit = 8; digit > 0; digit--)
  {
    byte c = buf [8 - digit];
    if (c == ' ' )
      c = 0xF;  // code for a blank
    else
      c -= '0';
    sendByte (digit , c);
  }
}  // end of number

unsigned int i=99990000;

void loop ()
{
  number (i++);
  if (i > 99999999) i = 0;
  delay (1);
}  // end of loop

Credits

paolo carnovale

paolo carnovale

5 projects • 10 followers
Peter Oakes

Peter Oakes

6 projects • 37 followers
Electronics Engineer, Programmer, and love to make Videos on Electronics Engineering related stuff and post on my YouTube Channel "thebreadboardca"

Comments