Week 9 - Output Devices¶
Assignments | Week 9 | Output Devices
Group assignment
- Measure the power consumption of an output device.
- Document your work on the group work page and reflect on your individual page what you learned.
Individual assignment
- Add an output device to a microcontroller board you’ve designed and program it to do something.
Learning outcomes
- Demonstrate workflows used in controlling an output device(s) with MCU board you have designed.
Introduction¶
In this documentation I will show the programming processes for an output device. I’ll be using the PCB that I made in the previous weeks:
Hero Shot¶
Programming an output device¶
What do I have to achieve?¶
My output board contains:
- A Button Omron B3SN
- An RGB light chip : Adafruit’s Neopixel - aka WS2812B (also WS2811/WS2812/WS2813, also supported in lo-speed mode) - a 3 wire addressable LED chipset
I’m going to program a little memory game, using the Serial output as interface and the board as an input (Button) + output (RGB LED) device.
What did I do?¶
Materials:
- My PCB
- ArduinoIDE for coding
- Google Chrome, a web browser for research and datasheet
- KICAD to have access to the PCB’s design and pinout
In KICAD I opened my PCB design
I tried to find a way to export the pinout of the pcb in KICAD. But I didn’t find it. So I just modified the F.Silkscreen layer in KICAD’s PCB editor. I double click an electronic component and changed
This board works with a programmer. I’ll be using this Serial-UPDI programmer, designed and made by my instructor Stephane: https://gitlabsu.sorbonne-universite.fr/mullerst/serialupdi-programmer
I used an existing code to test if my button work
#define BUTTON_PIN 4
int counter=0;
void setup()
{
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop()
{
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
counter++;
Serial.print("Button is pressed ");
Serial.print(counter);
Serial.println("times");
}
delay(10);
}
But the ArduinoIDE printed an error as follow:
Sketch uses 1666 bytes (46%) of program storage space. Maximum is 3584 bytes.
Global variables use 61 bytes (23%) of dynamic memory, leaving 195 bytes for local variables. Maximum is 256 bytes.
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x30
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x20
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x30
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x20
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x30
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x20
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x30
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x20
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x30
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x20
Failed uploading: uploading error: exit status 1
Then I checked the USB connection, and make sure I chose the right board name of the microcontroller. Then I found what was wrong, I didn’t select the programmer. So I clicked on Tool > Programmer > SerialUPDI - SLOW:57600 baud
The upload went smoothly, and the copy/paste program worked (which is a good sign!)
The program works as I press the buttun on my board!
Now I would like to make the RGB led
I searched on internet: how to program WS2812B. I found a website, and the most important library to this chip.
I found a documentation by RishabhL on Instructables.com : https://www.instructables.com/Basic-of-FastLED/
I followed the documentation by installing the FastLed library and added the basic instructions to write an led: https://github.com/FastLED/FastLED/wiki/Basic-usage
#include <FastLED.h>
#define BUTTON_PIN 2 // Analog/Digital Pin num
#define NUM_LEDS 1 // Number of led that are being used
#define DATA_PIN 3 // Analog/Digital Pin number of the rgb led
int counter=0;
CRGB leds[NUM_LEDS];
void setup()
{
Serial.begin(9600);
FastLED.addLeds<WS2812B, DATA_PIN>(leds, NUM_LEDS);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop()
{
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
leds[0] = CRGB::Red;
FastLED.show();
counter++;
Serial.print("Button is pressed ");
Serial.print(counter);
Serial.println("times");
}
delay(100);
}
The first test failed.
warning: #warning "No pin/port mappings found, pin access will be slightly slower. See fastpin.h for info." [-Wcpp]
#warning "No pin/port mappings found, pin access will be slightly slower. See fastpin.h for info.
note: #pragma message: No hardware SPI pins defined. All SPI access will default to bitbanged output
# pragma message "No hardware SPI pins defined. All SPI access will default to bitbanged output"
In file included from c:\Users\lauri\Documents\Arduino\libraries\FastLED\src/FastLED.h:62:0,
from C:\Users\lauri\Documents\Arduino\output_rgb\output_rgb.ino:1:
c:\Users\lauri\Documents\Arduino\libraries\FastLED\src/fastpin.h: In instantiation of 'class FastPin<3>':
c:\Users\lauri\Documents\Arduino\libraries\FastLED\src/platforms/avr/clockless_trinket.h:107:49: required from 'class ClocklessController<3, 5, 13, 8, (EOrder)66, 0, false, 10>'
c:\Users\lauri\Documents\Arduino\libraries\FastLED\src/chipsets.h:740:7: required from 'class WS2812Controller800Khz<3, (EOrder)66>'
c:\Users\lauri\Documents\Arduino\libraries\FastLED\src/FastLED.h:139:52: required from 'class WS2812B<3, (EOrder)66>'
c:\Users\lauri\Documents\Arduino\libraries\FastLED\src/FastLED.h:368:39: required from 'static CLEDController& CFastLED::addLeds(CRGB*, int, int) [with CHIPSET = WS2812B; unsigned char DATA_PIN = 3; EOrder RGB_ORDER = (EOrder)66]'
C:\Users\lauri\Documents\Arduino\output_rgb\output_rgb.ino:9:56: required from here
c:\Users\lauri\Documents\Arduino\libraries\FastLED\src/fastpin.h:261:2: error: static assertion failed: Invalid pin specified
static_assert(validpin(), "Invalid pin specified");
^~~~~~~~~~~~~
exit status 1
Compilation error: exit status 1
To debug this, I checked several times the PIN number
With desperation, I closed and reopened ArduinoIDE, I tried to replace the PIN number by 1, 2, 3, 4, 5, 6, 7, 8. The error remains the same. Now I can be sure that the problem does not come from the code, neither ArduinoIDE. I’ll seek help from my instructor when I’ll be in the lab.
Meanwhile, I found an github issue online, someone has faced the same problem as me. https://github.com/FastLED/FastLED/issues/973
The answer to this issue : “Thanks for opening this issue. Probably it’s a small piece of work if you’d like to try adding the port/pin mappings yourself. If that’s not something you’re familiar with, perhaps someone else can help out here.”
I browsed the datasheet of ATtiny412
- [datasheet_ATtiny412]](https://ww1.microchip.com/downloads/en/DeviceDoc/40001911A.pdf)
- https://community.particle.io/t/fastled-with-ws2812b-leds-on-photon/18451/13
My instructor took a look on the code and checked with me if the microcontroller I’m using is supported by the library. The answer: No.
He reccomanded a library : tinyNeoPixel
I was quite confused by the basic code of the official documentation, because then I found a documentation page of a Fab’Academy 2020 student (William from Charlotte)
I copy-paste the code and modified the pin number.
#include <tinyNeoPixel.h>
#define PIN 3
// Creating a tinyNeoPixel object: Parameters in the constructor are (# of LEDS, PIN, Color System)
tinyNeoPixel strip = tinyNeoPixel(5, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show();
}
void loop() {
strip.setPixelColor(0, strip.Color(0,0,255));
strip.show();
delay(50);
}
Thanks to Stéphane!