How to communicate with ATTiny85/Digispark via USB on PC? - c++

Recently I'm trying to make a simple LED controller with ATTiny85/Digispark.
I tried to use DigiCDC lib to perform data IO
but it does not work on my PC (win10 x64).
test code:
#include <DigiCDC.h>
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
SerialUSB.begin();
SerialUSB.println("hello world");
}
void loop()
{
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
delay(200);
digitalWrite(0, LOW);
digitalWrite(1, LOW);
delay(800);
SerialUSB.println("ping");
int ava = SerialUSB.available();
int buffer[ava];
// read buffer
for(int step = 0; step < ava; step++)
buffer[step] = SerialUSB.read();
// write buffer back
for(int step = 0; step < ava; step++)
SerialUSB.print(buffer[step]);
SerialUSB.println("==line end==");
delete buffer;
}
Official demo mentioned here (Arduino IDE - Files - Examples - DigiCDC - Echo) also did not work.
Once program is compiled and uploaded onto board, Windows shows a "Unknown USB device" notification. And no usable serial port devices can be found.
Are there missing some drivers?
Or DigiCDC lib is simply not working on Win10?
Or I should use another lib to achieve communication between PC and ATTiny85/Digispark via USB?

I would first check if the Blink sketch works to rule out driver issues. The ATTINY85 I have uses PIN '1' for the onboard LED. For reference, I got my devices here:
https://www.amazon.com/gp/product/B0836WXQQR/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&th=1
Also, make sure you don't plug the ATTINY into the USB port until the Arduino IDE asks for it. So, just click the 'Upload' button and wait for it to compile and ask for the board. I'm assuming you know how to set up the IDE to work with the ATTINY. If not, this tutorial goes over that part:
https://www.instructables.com/Attiny85-USB-Development-Board-LED-Blinking-With-A/

Related

Arduino USB Serial communication is throwing error, "avrdude: ser_open(): can't set com-state for "\\.\COM3"

I am learning to program Arduino Uno board. To be honest, I am an absolute beginner to it and I just started learning it today. I have finished my first lesson on blinking an LED light. That went well without any issues.
Now, I am trying to send data from my Arduino board to another device through USB serial port communication. For now, I am following this tutorial to get the serial port software working. https://www.arduino.cc/en/Tutorial/LibraryExamples/SoftwareSerialExample.
So I am trying to compile the following code in the IDE before uploading to the Arduino board.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Native USB only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(38400);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
When I compiled it, I am getting the following error.
An error occurred while uploading the sketch
avrdude: ser_open(): can't set com-state for "\\.\COM3"
What is wrong with my code and how can I fix it?

Using servo library in tinkercad causing weird behaviour

I am using analogWrite() in my code. Everything works fine when I am not using the servo library. In other words, everything is fine when I disable this line //servo_9.attach(9); If I enable this line, the PWM will be wrong and weird. Any idea why the Tinkercad simulator is acting weird?
#include <Servo.h>
int led =9;
Servo servo_9;
void setup() {
servo_9.attach(9);
Serial.begin(9600);
}
void loop() {
for(int n=0;n<255;n++){
analogWrite(led,n);
delay(5);
}
}
This video shows what is happening:
click here
The documentation for the servo library clearly states.
On boards other than the Mega, use of the library disables
analogWrite() (PWM) functionality on pins 9 and 10, whether or not
there is a Servo on those pins.

Setting up Serial USB communication between STM32 and PC with Mbed library

I hava an STM32 f401RE. I am using Mbed library for setting up a conexion from STM32 to PC. I want to send via serial a char sequence to the board. As an answer i expect a blinking LED. E.g: led1 results in LED ON, led2 results in LED OFF.
The problem is that i don't know how to set the port for the connection.
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
USBSerial serial;
int main(void) {
while(1)
{
serial.printf("I am a virtual serial port\r\n");
wait(1.0);
}
}
You can use the USBSerial interface to emulate a serial port over USB. You can use this serial port as an extra serial port or as a debug solution. It also communicates between Mbed and a computer.
I would like to do all the above(even thogh i don't know what does emulate a serial port over USB. What is that Virtual USB?).
I see that USBSerial constructor takes
USBSerial (bool connect_blocking=true, uint16_t vendor_id=0x1f00, uint16_t product_id=0x2012, uint16_t product_release=0x0001). And i think i need to modify some of this adresses. The problem is that on Windows the ports are represented in Device Manager with COMxx and on Linux like ttyACMxx. How would i transform this in hexa adresses - is this what i have to do?
You should not have to transform anything or mess with the USB product_id or vendor_id, an mbed serial port should show as any other serial port so if it doesn't for you it means you are having driver issues.
On most recent Linux distros the device should show something similar to the following kernel messages:
cdc_acm 5-2:1.1: ttyACM0: USB ACM device
usbcore: registered new interface driver cdc_acm
cdc_acm: v0.26:USB Abstract Control Model driver for USB modems and ISDN adapters
On Windows, you will probably need to install drivers. After you do that, the serial port should show as mbed Serial Port (COMx) on your Device Manager. There are many places you can get troubleshooting help, see here, for instance.
The fact that you are getting nothing on both Windows and Linux makes one wonder if you are using the right cable (some USB cables work only for charging and are no good for your purposes, and some others simply fail after a while). I would first make sure your cable works with other devices (obviously not for charging only). There is also the possibility your board went (or came from the factory) bad, but that's quite unlikely.
I just found this approach and it is working. The thing that i don't understand is why on my pc i get this message: b'Hello World!\n'
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
int main() {
pc.baud(9600);
while(1)
{
pc.printf("Hello World!\n");
wait(0.1);
}
}
Ignore that 'b'. Your device is not seeing that 'b'. It is just being printed by serial terminal utility. Also I would like to mention what I got from your question is, you want to send some data from PC to board over Serial and if device receives that data, it should start blinking the LED. If that is correct, use the code below:
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut led(LED1); // If blinking doesn't work with LED1, Check the pin map for your board and pass the LED pin instead of LED1
char token = 'a'; // This is the character that you should send to trigger blinking
bool startBlinking = false;
int main() {
pc.baud(9600);
while(1)
{
if (pc.getc() == token) {
startBlinking = true;
}
if (startBlinking) {
led = 1;
wait(0.2);
led = 0;
wait(0.8);
}
}
}

Arduino Uno Wifi library not working

I have recently purchased an Arduino Uno WIFI. It says it already has the ESP8266 wifi module integrated making it WIFI ready. I have successfuly connected to my wifi and wifi console. I have also used the test WebServer Blink test to play around with the pin 13 rest api commands. The problem im having is going beyond this example. I searched for WIFI documentation but can only find this documentation for the WIFI-Shield which is not working for my arduino.
I see in the example they import the #include <ArduinoWiFi.h> but i cannot find this libraries documentation. Is there anyother library I can use with this new arduino wifi? Does anyone have experience with this? I have tried to use the #include <WIFI.h> but it says that I don't have the wifi sheild.
ERROR:
WebServerBlink.ino:14:23: error: 'class ArduinoWifiClass' has no member named 'status'
CODE:
#include <Wire.h>
#include <ArduinoWiFi.h>
/*
on your borwser, you type http://<IP>/arduino/webserver/ or http://<hostname>.local/arduino/webserver/
http://labs.arduino.org/WebServerBlink
*/
void setup() {
pinMode(13,OUTPUT);
Wifi.begin();
Wifi.println("WebServer Server is up");
Wifi.println(Wifi.status()); //Line 14:23:: This will not work
}
void loop() {
while(Wifi.available()){
process(Wifi);
}
delay(50);
}
void process(WifiData client) {
// read the command
String command = client.readStringUntil('/');
// is "digital" command?
if (command == "webserver") {
WebServer(client);
}
if (command == "digital") {
digitalCommand(client);
}
}
void WebServer(WifiData client) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<head> </head>");
client.print("<body>");
client.print("Click<input type=button onClick=\"var w=window.open('/arduino/digital/13/1','_parent');w.close();\"value='ON'>pin13 ON<br>");
client.print("Click<input type=button onClick=\"var w=window.open('/arduino/digital/13/0','_parent');w.close();\"value='OFF'>pin13 OFF<br>");
client.print("</body>");
client.println("</html>");
client.print(DELIMITER); // very important to end the communication !!!
}
void digitalCommand(WifiData client) {
int pin, value;
// Read pin number
pin = client.parseInt();
// If the next character is a '/' it means we have an URL
// with a value like: "/digital/13/1"
if (client.read() == '/') {
value = client.parseInt();
digitalWrite(pin, value);
}
// Send feedback to client
client.print(F("Pin D"));
client.print(pin);
client.print(F(" set to "));
client.print(value);
client.print(EOL);
}
There is a big difference between Arduino Uno WIFI (http://www.arduino.org/products/boards/arduino-uno-wifi) from arduino.org and the Arduino WiFi Shield (www.arduino.cc/en/Main/ArduinoWiFiShield) from arduino.cc.
This is a good starting point for your Arduino Uno WIFI:
http://www.arduino.org/learning/getting-started/getting-started-with-arduino-uno-wifi
The next important point is, that you need to use Arduino 1.7 (from arduino.org) especially for OTA programming. Arduino 1.6.x from arduino.cc doesn't work.
Unfortunately they don't really develop their arduinowifi-library well.
I had the same problem on Linux IDE 1.8.1 and I solved it like this:
get https://github.com/arduino-org/Arduino/tree/master/libraries/ArduinoWiFi
add it in .....arduino-1.8.1/libraries/ArduinoWiFi/
restart the IDE. You shall be able to open and run the example sketch under File->Examples->ArduinoWiFi.
I suppose that it will work with any IDE on any platform.

xbee arduino communication programming

I tried testing the system and I’m not sure if the problem is with the xbee’s, the transmitting code, or the recieveing code. Before I post my code I will explain what we are doing with the signals. We have three analog signals that will be sent serially through one xbee using an arduino and xbee shield. We want to send these signals to the receiving xbee where the arduino will output these signals to be connected to a third arduino through wires to be used in a Simulink program. We are using an arduino mega for the transmitting side and an arduino uno for the receiving side. I was told I need to do serial streaming but I’m not sure how that’s done. I understand the xbee and arduinos both digitize signals but we are hoping to get a signal very similar to the analog signals we are transmitting. Any amount of help is greatly appreciated!!
This is how I have my xbees configured (series 1) both in AT mode:
Transmitting Xbee:
Channel:10
Pan id: 1234
MY: 10
DL: 11
Receiving Xbee:
Channel:10
Pan ID: 1234
MY: 11
DL: 10
transmitting Arduino code:
void setup() {
Serial.begin(9600);
}
void loop() {
// read the input on analog pins
int sensorValue1 = analogRead(A0);
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
// print out the value you read:
Serial.println(sensorValue1);
Serial.println(sensorValue2);
Serial.println(sensorValue3);
delay(1);
}
Receiving Arduino code:
int received1=8;
int received2=9;
int received3=10;
void setup(){
pinMode(received1, OUTPUT);
pinMode(received2, OUTPUT);
pinMode(received3, OUTPUT);
Serial.begin(9600);
}
void loop(){
if(Serial.available() )
{
byte output1 = Serial.read();
byte output2 = Serial.read();
byte output3 = Serial.read();
digitalWrite(received1, HIGH);
digitalWrite(received2, HIGH);
digitalWrite(received3, HIGH);
}
}
It sounds like you're using the XBee modules in "AT mode" or "transparent serial" mode where anything received on the serial port of module A is sent out of the serial port of module B, and vice versa.
If that's the case, it may help to do your initial development with the serial ports of your two devices connected directly to each other. Work out your serial protocol there, and then try to run it with the XBee modules in place as a serial cable replacement.
Consider the format of the data that you're sending, and how you will process it on the other end. How will you separate the readings and identify which analog input they belong to? With your current code, you print the readings on separate lines, but it won't be clear which is A0. Maybe you want to send them on a single line with a comma in between each reading?
On the receiving end, you need to convert the text back to an integer using a C function like atoi() or strtoul().
If you're trying to create an analog output on the Arduino, it might be possible with a digital output that's using PWM (pulse width modulation). This Instructable does a decent job of describing that concept.